2016-12-16 119 views
10

我试图简单地显示用户在地图上的位置,但我需要在应用启动时将地图放大到当前位置,但我不知道为什么地图不放大一切,是这样的:MapKit放大到用户当前位置

enter image description here

下面是代码:

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    var locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.delegate = self 
     DispatchQueue.main.async { 
      self.locationManager.startUpdatingLocation() 
     } 
    } 

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
     let location = locations.last as! CLLocation 
     let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
     var region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)) 
     region.center = mapView.userLocation.coordinate 
     mapView.setRegion(region, animated: true) 
    } 
+0

删除'region.center = mapView.userLocation.coordinate' – shallowThought

+0

@shallowThought我没有,但什么都没有改变 –

回答

11

我面临着类似的问题和浪费的4天想着怎么回事错了。终于解决了与viewDidLoad方法创建的这些代码行:

//Zoom to user location 
    let noLocation = CLLocationCoordinate2D() 
    let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200) 
    mapView.setRegion(viewRegion, animated: false) 

ViewDidLoad方法添加这些新的变化代码:

override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.delegate = self 

     //Check for Location Services 
     if (CLLocationManager.locationServicesEnabled()) { 
      locationManager = CLLocationManager() 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestAlwaysAuthorization() 
      locationManager.requestWhenInUseAuthorization() 
     } 
     locationManager.requestWhenInUseAuthorization() 
     if CLLocationManager.locationServicesEnabled() { 
      locationManager.startUpdatingLocation() 
     } 
     //Zoom to user location 
     let noLocation = CLLocationCoordinate2D() 
     let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200) 
     mapView.setRegion(viewRegion, animated: false) 

     DispatchQueue.main.async { 
      self.locationManager.startUpdatingLocation() 
     } 
} 

希望这有助于解决您的问题。随时发表评论,如果任何进一步的问题。谢谢

+0

因为的LocationManager未初始化第一次调用这段代码失败。远程线路: locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.delegate = self – Gadzair

+1

先阅读问题,然后发表评论 –

+0

为什么line'locationManager.requestWhenInUseAuthorization()'重复? – Antek

11

这是Swift 3,XCode 8.2的另一种方法。首先,写出一个辅助函数:

let homeLocation = CLLocation(latitude: 37.6213, longitude: -122.3790) 
let regionRadius: CLLocationDistance = 200 
func centerMapOnLocation(location: CLLocation) 
{ 
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
              regionRadius * 2.0, regionRadius * 2.0) 
    mapView.setRegion(coordinateRegion, animated: true) 
} 

然后,调用在viewDidLoad()

mapView.showsUserLocation = true 
centerMapOnLocation(location: homeLocation) 

这将启动应用程序与在放大的变量指定的位置

+2

它会更有帮助,如果'homeLocation'会被代码自动找到。 – nyxee

1

代码:

import UIKit 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

@IBOutlet weak var mapview: MKMapView! 

let locationmanager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    mapview.mapType = MKMapType.standard 

    let location = CLLocationCoordinate2DMake(22.4651, 70.0771) 

    let span = MKCoordinateSpanMake(0.5, 0.5) 
    let region = MKCoordinateRegionMake(location, span) 
    mapview.setRegion(region, animated: true) 

    let annonation = MKPointAnnotation() 
    annonation.coordinate = location 
    annonation.title = "Chandi Bazar" 
    annonation.subtitle = "Jamnagar" 
    // 
    mapview.addAnnotation(annonation) 

    self.locationmanager.requestWhenInUseAuthorization() 

    if CLLocationManager.locationServicesEnabled() 
    { 
     locationmanager.delegate = self 
     locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationmanager.startUpdatingLocation() 
    } 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate 
    print("locations = \(locValue.latitude) \(locValue.longitude)") 

    locationmanager.stopUpdatingLocation() 
} 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
{ 
    if (annotation is MKUserLocation) 
    { 
     return nil 
    } 

    let annotationidentifier = "Annotationidentifier" 

    var annotationview:MKAnnotationView 
    annotationview = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationidentifier) 

    let btn = UIButton(type: .detailDisclosure) 

    btn.addTarget(self, action: #selector(ViewController.hirenagravat(sender:)), for: .touchUpInside) 

    annotationview.rightCalloutAccessoryView = btn 

    annotationview.image = UIImage(named: "images (4).jpeg") 

    annotationview.canShowCallout = true 

    return annotationview 
} 

func hirenagravat(sender:UIButton) 
{ 
    let fvc = storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController 
    self.navigationController?.pushViewController(fvc!, animated: true) 
} 
0

试用MKMapViewDelegate功能:

var isInitiallyZoomedToUserLocation: Bool = false 

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
    if !isInitiallyZoomedToUserLocation { 
     isInitiallyZoomedToUserLocation = true 
     mapView.showAnnotations([userLocation], animated: true) 
    } 
} 
0

当你设置区域 - >你不能缩放地图了。下面来修复

func yourFuncName() { 
//this is global var 
regionHasBeenCentered = false 
if !self.regionHasBeenCentered { 
    let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01) 
    let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(_cllocationOfUserCurrentLocation!.coordinate.latitude, _cllocationOfUserCurrentLocation!.coordinate.longitude) 
    let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span) 

    self.mapView.setRegion(region, animated: true) 
    self.regionHasBeenCentered = true 
    } 

    self.mapView.showsUserLocation = true 
}