I am facing an issue while displaying google maps in a table view cell with swift. I want to display check In (Latitude and Longitude) based on this I want to display google map in table view. I will latitude and longitude from server if no location available means I will get null. so, In one scenario I am getting correct, but while reloading the top-level I am getting map when and where their latitude and longitude is null also. Please guide me.
-
It sounds like you might want to lazily instantiate and insert the Google Map view programmatically. `dequeueReusableCell` may give you cells that already have the map instantiated. However, I don't know if it's very clear what you're asking, and I would also caution against instantiating too many of these expensive views. Perhaps there is a solution that involves generating static images showing the location you want users to see. – Rikki Gibson Aug 07 '16 at 17:11
3 Answers
A map view is an expensive view to instantiate. Even when using dequeueReusableCellWithIdentifier
it will still be resource-heavy with a large number of items.
I believe using a way to generate a static map image from your longitude/latitude combination is your best bet.
You can take a look here and you can see how to easily construct an API call to many popular map providers such as Google for a static image map like for example:
Also worth mentioning that some of those providers might have limitations (Like Google might need an API key in case of high traffic). So choose wisely after some research.

- 128
- 1
- 6
-
-
You can probably use `MKMapSnapshotter`. It needs some options like the region to snapshot and stuff like that and it will give you a UIImage for an output. It's available for iOS 7 and above and also works asynchronously. – Hobayier Sallam Jun 02 '17 at 09:38
-
@DHEERAJ for something like Google maps you can take a look at this section: https://developers.google.com/maps/documentation/static-maps/intro#Markers Other providers might have similar capabilities. As for Apple Maps and `MKMapSnapshotter` I'm afraid it's not that simple, you'll have to draw the snapshot and the pin in a graphic context. You can find a solution for it here on stack overflow, as in here: https://stackoverflow.com/a/42773351/2431449 – Hobayier Sallam Oct 09 '17 at 07:45
Looks like you are facing two issues
1) Already mentioned one "Map appears even when data is nil"
2) Performance issue (It is not mentioned here though)
The first one is due to dequeueing of cell. When you reload table , the cell will be reused(Not created again).
When you return your cell for row in
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
the cell with map view may get return, that is why you get unwanted map there. You should make it nil to prevent this.
2:
When came to performance, its a not the right approach to show mapview in every cell. An alternate approach is to make image out of your map url
use this
let mapUrl: String = NSURL(string:"YourMapURL")
let mapImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))

- 313
- 4
- 12
let latitude:Double = 17.3850
let longitude:Double = 78.4867
let imageURL = NSURL(string: "http://maps.googleapis.com/maps/api/staticmap?center=\(latitude),\(longitude)&zoom=12&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true")
let imagedData = NSData(contentsOfURL: imageURL!)!
staticMapImgView.image = UIImage(data: imagedData)

- 4,851
- 5
- 38
- 73