Map button refresh location

自闭症网瘾萝莉.ら 提交于 2019-11-30 14:59:36

As @Orkhan said you can do it in this way.

If you want to do the action you just simple ctrl-drag to viewController and select "Action".

After that you can add code to the handler.

    var location: CLLocation!
    @IBAction func refreshLocation(sender: AnyObject) {

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    self.location = locations.last as CLLocation
}

Try the following 3 line code:

@IBAction func refresh(sender: AnyObject) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}

EDIT

Check the following:

Can you see the dot on the left side of the method?

I want see your connections inspector. Refresh button and refresh method are connected correctly?

You can update your location with this function:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

If your target is iOS 8, you need to add the NSLocationAlwaysUsageDescription key in your Info.plist.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!