2015-01-15 3 views
1
  • Я новичок в быстром.
  • , пожалуйста, подскажите мне, как отображать location на карте, когда пользователь вводит какое-либо место в текстовое поле.
  • с помощью этой строки текстового поля, как отобразить это местоположение на map. Это мой кодКак отобразить определенное пользователем местоположение

    import UIKit 
    import CoreLocation 
    import MapKit 
    class DetailViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 
    
    var manager:CLLocationManager! 
    var myLocations: [CLLocation] = [] 
    
    
    required init(coder aDecoder: NSCoder) 
    { 
        super.init(coder: aDecoder) 
    } 
    
    override func viewDidLoad() { 
        super.viewDidLoad() 
    //Setup our Location Manager 
        manager = CLLocationManager() 
        manager.delegate = self 
        manager.desiredAccuracy = kCLLocationAccuracyBest 
        manager.requestAlwaysAuthorization() 
        manager.startUpdatingLocation() 
        //Setup our Map View 
        theMap.delegate = self 
        theMap.mapType = MKMapType.Satellite 
        theMap.showsUserLocation = true 
    
    } 
    func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { 
        theLabel.text = "\(locations[0])" 
        myLocations.append(locations[0] as CLLocation) 
    
        let spanX = 0.007 
        let spanY = 0.007 
        var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) 
        theMap.setRegion(newRegion, animated: true) 
        if (myLocations.count > 1){ 
         var sourceIndex = myLocations.count - 1 
         var destinationIndex = myLocations.count - 2 
         let c1 = myLocations[sourceIndex].coordinate 
         let c2 = myLocations[destinationIndex].coordinate 
         var a = [c1, c2] 
         var polyline = MKPolyline(coordinates: &a, count: a.count) 
         theMap.addOverlay(polyline) 
        } 
    } 
    

благодаря

ответ

0

Я предполагаю, что это, как ваш контроллер представления является designed.When пользователь нажимает на кнопку «Найти местоположение» кнопку мы преобразовать текст, введенный пользователем в адрес и поместить аннотацию на карту. enter image description here

@IBOutlet var userinput: UITextField! 
@IBAction func findlocation(sender: AnyObject) { 
if(userinput.text == "") 
{ 
//location cannot be empty.Show an alert to notify user 
} 
else 
{ 
    var address:String = userinput.text as String // 1 Infinite Loop, CA, USA 
var geocoder = CLGeocoder() 
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in 
if let placemark = placemarks?[0] as? CLPlacemark { 
     self.mapView.addAnnotation(MKPlacemark(placemark: placemark)) 
} 
}) 
} 
} 
Смежные вопросы