Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map in tableView

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.

like image 605
Uma Madhavi Avatar asked Aug 05 '16 16:08

Uma Madhavi


2 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:

http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true

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.

like image 132
Hobayier Sallam Avatar answered Nov 15 '22 07:11

Hobayier Sallam


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))
like image 23
renjithr Avatar answered Nov 15 '22 08:11

renjithr