Map showing place of search swift

2

Well, I'd like to know how I can get my map to show the place I've been looking for 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

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

Update 1:

First I created a Geocoder:

var geocoder = CLGeocoder()

Then I used the method of fetching an address string and put it in the placemark variable. And using the properties available in this variable I used the placemark.location.coordinate.latitude and the 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

}
    
asked by anonymous 17.01.2015 / 02:38

2 answers

2

How to map map search location using coordinates without external api

First I created a Geocoder:

var geocoder = CLGeocoder()

Then I used the method of fetching an address string and put it in the placemark variable. 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

}
    
18.01.2015 / 23:01
1

Well, as you first need to find the coordinates, maybe the answer is a bit broad, but in short, there is no method in which you give the address and it returns you latitude and longitude then you must first seek an alternative, such as Google Geocoding API . p>

It basically consists of passing an address and receiving a 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 at hand (after making a request with NSURLConnection and etc), to center this point on the map, create the object CLLocationCoordinate2D , define the region and then point 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, it looks something like this:

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

mapDisplay.addAnnotation(annotation)
    
17.01.2015 / 14:10