Map showing search location Swift

Asked

Viewed 838 times

2

Guys, I’d like to know how I can get my map to show the place that was searched and center the view at this point. I was able to do with the current location as follows:

self.mapDisplay.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true) //Linha de seguir o mapa

However I need with the result of the searched address, what I should use?

Update 1:

First I created a Geocoder:

var geocoder = CLGeocoder()

Then I used the method of fetching an address string and putting it into the Placemark variable. And using the properties available in this variable I used Placemark.location.coordinate.latitude and Placemark.location.coordinate.longitute:

 geocoder.geocodeAddressString(address,{(placemarks: [AnyObject]!, error: NSError!) -> Void in
                if let placemark = placemarks?[0] as? CLPlacemark { 

 let location = CLLocationCoordinate2D(latitude: placemark.location.coordinate.latitude, longitude: placemark.location.coordinate.longitude)
                    self.mapView.region = MKCoordinateRegionMakeWithDistance(location, 350, 350)
                    self.mapView.centerCoordinate = location

}
  • You already have this method of finding coordinates from the address or is it from there that you need?

  • It is from there that I need, I do not possess the method.

2 answers

2


How to make the map show search location using coordinates without external api

First I created a Geocoder:

var geocoder = CLGeocoder()

Then I used the method to find an address string and put it in the variable Placemark. And using the properties available in this variable I used the

placemark.location.coordinate.latitude e o placemark.location.coordinate.longitute:

 geocoder.geocodeAddressString(address,{(placemarks: [AnyObject]!, error: NSError!) -> Void in
                if let placemark = placemarks?[0] as? CLPlacemark { 

 let location = CLLocationCoordinate2D(latitude: placemark.location.coordinate.latitude, longitude: placemark.location.coordinate.longitude)
                    self.mapView.region = MKCoordinateRegionMakeWithDistance(location, 350, 350)
                    self.mapView.centerCoordinate = location

}
  • If this is the right solution mark it

1

Well, since you first need to find the coordinates, maybe the answer is a little broad, but in short, in the iOS there is no method that you pass the address and it returns you latitude and longitude so you first need to seek an alternative, as the Google Geocoding API.

Basically it consists of you passing an address and receiving one JSON, which has several parameters and among them latitude and longitude. For example:

http://maps.googleapis.com/maps/api/geocode/json?address=Av+Paulista+4000,+Sao+Paulo,+SP&sensor=true

If you access this address you will find the parameters you need and with them in hand (after making a request with NSURLConnection etc.), to center this point on the map, create the object CLLocationCoordinate2D, define the region and then point to the center. So:

let location = CLLocationCoordinate2D(latitude: lat, longitude: lng)

mapDisplay.region = MKCoordinateRegionMakeWithDistance(location, 900, 900)
mapDisplay.centerCoordinate = location

Well, that’s basically the way you should go. If you need more details about the request, you can do a search or create another question, I believe there are already some good examples with this type of service request.

To put a marker on the map, something like this:

let annotation: MKPointAnnotation! = MKPointAnnotation()
annotation.coordinate = location

mapDisplay.addAnnotation(annotation)
  • But without using the googleapi what the method would be if I had the coordinates?

  • With the coordinates, just use this code, where lat and lng in the first line are its values in double, which will center the map.

  • I managed without needing google api, I will demonstrate my procedure when I edit the question.

  • You are right, it is even possible to get the coordinates without using external service. My mistake, your solution is even better.

  • Thank you, but it would not be possible without your help! There is some way to put a "pin" in this string now?

  • Yes, very simple. I made an edit on my reply on how to add a bookmark.

  • @Skal Could you post your solution for geocoding as answer instead of leaving in the question itself? It is more consistent with the format of the site, and should help other people with the same question. Thank you!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.