使地图缩放到用户位置和注释(swift 2) [英] Making the map zoom to user location and annotation (swift 2)

查看:19
本文介绍了使地图缩放到用户位置和注释(swift 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mapkit.我希望地图缩放以显示用户的位置和注释点,而不是仅缩放到用户的当前位置.

I am working with mapkit. I want the map to zoom to show the user's location and the annotated point, instead of just zooming to the user's current location.

目前我有:

                            let annotation = MKPointAnnotation()
                            annotation.coordinate = CLLocationCoordinate2DMake(mapLat, mapLon)
                            annotation.title = mapTitle
                            self.map.addAnnotation(annotation)

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLoction: CLLocation = locations[0]
    let latitude = userLoction.coordinate.latitude
    let longitude = userLoction.coordinate.longitude
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    self.map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
}

它只是放大到用户的位置,然后锁定在该位置.当我尝试滚动时,它会直接跳回到用户的位置.我究竟做错了什么?如何允许用户在地图上缩放或移动而不跳回当前位置?

It just zooms to the user's location and it gets locked on the location. When I try to scroll, it just jumps right back to the user's location. What am I doing wrong? How do I allow the user to zoom or move on the map without it jumping back to the current location?

我正在尝试放大以在同一视图上显示注释和用户的位置.即使注释很远,我也希望它可以缩放以显示用户和注释.

I’m trying to zoom in to show the annotation and the user's location on the same view. Even if the annotation is far away, I want it to zoom to show both the user and annotation.

推荐答案

它只是直接跳回到用户的位置,因为 didUpdateLocations 方法被多次调用.有两种解决方案.

It just jumps right back to the user's location, because didUpdateLocations method is called many times. There are two solutions.

1) 使用 requestLocation

如果您使用requestLocation 方法而不是startUpdatingLocationdidUpdateLocations 方法只会被调用一次

If you use requestLocation method instead of startUpdatingLocation, didUpdateLocations method is called only once

if #available(iOS 9.0, *) {
    locationManager.requestLocation()
} else {
    // Fallback on earlier versions
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLoction: CLLocation = locations[0]
    let latitude = userLoction.coordinate.latitude
    let longitude = userLoction.coordinate.longitude
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    self.map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
}

2) 使用标志

var isInitialized = false

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if !isInitialized {
        // Here is called only once
        isInitialized = true

        let userLoction: CLLocation = locations[0]
        let latitude = userLoction.coordinate.latitude
        let longitude = userLoction.coordinate.longitude
        let latDelta: CLLocationDegrees = 0.05
        let lonDelta: CLLocationDegrees = 0.05
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        self.map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
    }
}

这篇关于使地图缩放到用户位置和注释(swift 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆