How to get static image from google maps in iOS
the reply of @Ankit is right, but @Alexsander asked in Swift, so :
var staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(self.staticData.latitude),\(self.staticData.longitude)&\("zoom=13&size=\(2 * Int(mapFrame.size.width))*\(2 * Int(mapFrame.size.height))")&sensor=true"
var mapUrl: NSURL = NSURL(string: staticMapUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))!
var image: UIImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))
var mapImage: UIImageView = UIImageView(frame: mapFrame)
for swift 4
let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=\(self.finalLatitude),\(self.finalLongitude)&\("zoom=15&size=\(2 * Int(imgViewMap.frame.size.width))x\(2 * Int(imgViewMap.frame.size.height))")&sensor=true"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgViewMap.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
How to get static image from google maps in iOS in swift 3
Swift 3
let Width = 100
let Height = 200
let mapImageUrl = "https://maps.googleapis.com/maps/api/staticmap?center="
let latlong = "18.495651759752, 73.809987567365"
let mapUrl = mapImageUrl + latlong
let size = "&size=" + "\(Int(Width))" + "x" + "\(Int(Height))"
let positionOnMap = "&markers=size:mid|color:red|" + latlong
let staticImageUrl = mapUrl + size + positionOnMap
if let urlStr : NSString = staticImageUrl.addingPercentEscapes(using:String.Encoding.utf8)! as NSString{
// setImageFromURL
}
Load static google map into UIImageView?
Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.
First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.
So basically just call this on the url and you should be good to go:
// The URL you want to load (snipped for readability)
NSString *mapurl = @"http://maps.google.com/maps/api/staticmap?[...]";
// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.
not able to set map as image in uiimageview swift
Try this. Note you have to get API key from Google cloud
let API_Key = //Your API Key.
let url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=\(2 * Int(imgBanner.frame.size.width))x\(2 * Int(imgBanner.frame.size.height))&maptype=roadmap&key=\(API_Key)"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgBanner.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
Always check the link in browser to view whether it is working fine or not means the image is showing correctly without any Map error: g.co/staticmaperror
. If you find any staticmaperror
, please visit map-static-error-messages.
Sample image:
https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&key=AIzaSyAJgFSXOEIqBWIZ2vUcngjpX5fBBFmX6t0
Getting static image of Google Map
Static Maps may fit your use case: https://developers.google.com/maps/documentation/staticmaps/
But check the Terms of Use — both the usage limits (at that URL) and the Terms of Service 10.1.1(h).
Retrieve image from Google Maps in iOS
http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap%%5C&markers=size:mid%%257Ccolor:red%%7CNew+York&sensor=false
Just needed to edit some of the % \ etc to incorporate the UTF8 encoding, however if you use the encoding method karthika suggested, it breaks the search. All I did was hard code the string and removed the added encoding before the address and its working perfectly.
Google Static Maps for iOS
From an ip or from a device number of free request that can be made from google is 25000 request only per day after this you have to pay to google for it. So answer to your question is request from a particular ip is considered for calulating total requests.
Google static maps don´t load in iOS devices
Ran the code in your question and was able to replicate your problem. This is happening due to new restriction on using http connections instead of https on iOS. Just like I mentioned in my comment, replacing WWW
with UnityWebRequest
solved this problem.
Also, you are unnecessarily creating new textures each time with new Texture2D(1280, 1280, TextureFormat.RGB24, false);
is executed. This should be done with Unity's UI Component RawImage
. In this case, no unnecessary memory allocation.
All you have to do to the code in your question is to remove the WWW
API and use UnityWebRequest
. Also remove Renderer
or Texture2D
and use RawImage
to display your map. Below is a simple test script I used to test this. It runs on the both Editor
,iOS
and possibly all other platforms.
public class Gpsconnect : MonoBehaviour
{
public RawImage imageDisp;
public float fixLat = 42.3627f;
public float fixLon = -71.05686f;
public int zoom = 4;
public string key = "";
public Vector3 iniRef;
public Transform cam;
private Camera mycam;
private string url = "";
//Update Map with the corresponding map images for the current location ============================================
IEnumerator load()
{
yield return null;
mycam = Camera.main;
fixLat = ((360 / Mathf.PI) * Mathf.Atan(Mathf.Exp(0.00001567855943f * (cam.position.z - 3 * mycam.orthographicSize / 2 + iniRef.z)))) - 90;
fixLon = (18000 * (cam.position.x + iniRef.x)) / 20037508.34f;
//MAP ================================================================================
//Build a valid Google Maps tile request for the current location
url = "https://maps.google.com/maps/api/staticmap?center=" + fixLat + "," + fixLon + "&zoom=" + zoom + "&scale=2&size=640x640&style=feature:all|element:geometry|hue:0x00fff0|lightness:0|gamma:0.21|visibility:on&style=feature:all|element:labels|visibility:off&style=feature:landscape.man_made|element:geometry|color:0x133f42|visibility:on&style=feature:landscape.natural|element:geometry.fill|visibility:on|hue:0x00ffd2|saturation:35|lightness:0|gamma:0.5&style=feature:poi|element:geometry.fill|lightness:0|gamma:0.6|visibility:on&style=feature:poi.park|element:geometry|visibility:on|saturation:0|color:0x2e9470&style=feature:road|element:geometry.fill|visibility:on|color:0x05111a&style=feature:road|element:geometry.stroke|visibility:off&style=feature:transit|element:geometry|visibility:off" + "&sensor=false&key=" + key;
UnityWebRequest www = UnityWebRequest.GetTexture(url);
yield return www.Send();
Debug.Log("Done: ");
if (www.isError)
{
Debug.Log("Error while downloading image: " + www.error);
}
else
{
imageDisp.texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
}
}
void Start()
{
StartCoroutine(load());
}
}
Related Topics
How to Convert Video (In Gallery) to Nsdata? in Swift
Date Formats from Device Based on Locale
Google Maps Sdk Not Displaying Properly in Uiview
This Code Signature Is No Longer Supported When Building App on iPhone 12
Calculating the Broadcast Address in Objective-C
Swrevealviewcontroller - Rightviewcontroller
iOS 10: How to Show Incoming Voip Call Notification When App Is in Background
Expected Declaration Error Creating Array in Viewcontroller, Can't Work Out Why
Enforce Collectionview to Have Only 2 Rows
Multiple Unusernotifications Not Firing
Horizontally Scroll All Rows of Uicollectionview Together
Uitextview Linkable Label Accessibility Voice Over Issue
Change Tabview Color Based on Which Tab Is Selected -Swiftui