Ios 在应用程序运行时获取用户位置,并在使用按钮移动后重新居中

Ios 在应用程序运行时获取用户位置,并在使用按钮移动后重新居中,ios,swift,mkmapview,core-location,Ios,Swift,Mkmapview,Core Location,我正在做一个关于使用Swift 3的MapKit的项目。下面的代码所做的是,一旦我加载应用程序,地图将在默认情况下加载。然后,当用户按下按钮时,它会将您带到用户的位置 我想要解决这个问题的方法是,我希望它在应用程序运行时立即加载用户的位置,然后当用户决定在屏幕上移动时,她/他可以点击按钮,然后重新居中显示用户位置。比如它是如何在地图上工作的 import UIKit import MapKit import CoreLocation class ViewController: UIViewCo

我正在做一个关于使用Swift 3的MapKit的项目。下面的代码所做的是,一旦我加载应用程序,地图将在默认情况下加载。然后,当用户按下按钮时,它会将您带到用户的位置

我想要解决这个问题的方法是,我希望它在应用程序运行时立即加载用户的位置,然后当用户决定在屏幕上移动时,她/他可以点击按钮,然后重新居中显示用户位置。比如它是如何在地图上工作的

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    // connecting the map from the mainBoard and refering to it as "myMap".....
    @IBOutlet weak var myMap: MKMapView!

    @IBAction func refLocation(_ sender: Any) {
        manager.startUpdatingLocation()
    }

    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location  = locations.last {
            let span: MKCoordinateSpan = MKCoordinateSpanMake(0.0075,0.0075)
            let myLocation :CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
            myMap.setRegion(region, animated: true)
        }

        self.myMap.showsUserLocation = true

        manager.stopUpdatingLocation()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
    }
}


不清楚你在问什么

如果要获得单个位置更新,可以使用函数
func requestLocation()

您可以在视图控制器的viewDidLoad中创建一个位置管理器(并将自己设置为其代理),然后调用
requestLocation()
一次

然后在按钮操作中再次调用
requestLocation()


使您的didUpdateLocations方法始终重新居中于地图。这样,单击按钮,您将收到一个且只有一个更新,并且地图将在收到更新后立即重新居中。

不清楚您在问什么

如果要获得单个位置更新,可以使用函数
func requestLocation()

您可以在视图控制器的viewDidLoad中创建一个位置管理器(并将自己设置为其代理),然后调用
requestLocation()
一次

然后在按钮操作中再次调用
requestLocation()


使您的didUpdateLocations方法始终重新居中于地图。这样,单击按钮后,您将收到一个且只有一个更新,并且地图将在收到更新后立即重新居中。

请在Swift 3中尝试此代码。

import UIKit
import MapKit
import CoreLocation


class ViewController2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var currentLocation = CLLocation()
var currentLat: Double!
var currentLon: Double!
@IBOutlet weak var mapView: MKMapView!



override func viewDidLoad() {
    super.viewDidLoad()

    myLocation()
}


func myLocation()
{
    if currentLocation.coordinate.latitude == 0.00 || currentLocation.coordinate.longitude == 0.0
    {
        print("no location")
    }
    else
    {
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled()
        {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
        }

        let locManager = CLLocationManager()
        locManager.requestWhenInUseAuthorization()

        if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways)
        {
            currentLocation = locManager.location!
        }

        print("currentLongitude: \(currentLocation.coordinate.longitude)")
        print("currentLatitude: \(currentLocation.coordinate.latitude)")

        currentLon = currentLocation.coordinate.longitude
        currentLat = currentLocation.coordinate.latitude

        let span = MKCoordinateSpanMake(0.2, 0.2)
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
        mapView.setRegion(region, animated: true)
    }

}

@IBAction func MyLocationBtnPressed(_ sender: Any)
 {
    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
    mapView.setRegion(region, animated: true)
 }
}

请为Swift 3尝试此代码

import UIKit
import MapKit
import CoreLocation


class ViewController2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var currentLocation = CLLocation()
var currentLat: Double!
var currentLon: Double!
@IBOutlet weak var mapView: MKMapView!



override func viewDidLoad() {
    super.viewDidLoad()

    myLocation()
}


func myLocation()
{
    if currentLocation.coordinate.latitude == 0.00 || currentLocation.coordinate.longitude == 0.0
    {
        print("no location")
    }
    else
    {
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled()
        {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
        }

        let locManager = CLLocationManager()
        locManager.requestWhenInUseAuthorization()

        if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways)
        {
            currentLocation = locManager.location!
        }

        print("currentLongitude: \(currentLocation.coordinate.longitude)")
        print("currentLatitude: \(currentLocation.coordinate.latitude)")

        currentLon = currentLocation.coordinate.longitude
        currentLat = currentLocation.coordinate.latitude

        let span = MKCoordinateSpanMake(0.2, 0.2)
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
        mapView.setRegion(region, animated: true)
    }

}

@IBAction func MyLocationBtnPressed(_ sender: Any)
 {
    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
    mapView.setRegion(region, animated: true)
 }
}

您不必再次调用
startUpdatingLocation()
,因为MapView总是知道用户位置

替换

@IBAction func refLocation(_ sender: Any) {
    manager.startUpdatingLocation()
}


您不必再次调用
startUpdatingLocation()
,因为MapView总是知道用户位置

替换

@IBAction func refLocation(_ sender: Any) {
    manager.startUpdatingLocation()
}


我需要应用程序在应用程序运行时加载带有用户位置的地图。但是,我想让用户在需要时能够在地图上移动。一旦用户想要重新居中地图,用户将使用按钮重新居中用户位置。我希望这是清楚的。你会在发射时显示你的地图视图控制器吗?如果是这样,我描述的步骤将完全满足您的要求。(应用程序启动。它显示包含地图的视图控制器。视图控制器的
viewDidLoad()
方法启动位置管理器并请求一个位置。当位置传递时,地图视图控制器将地图集中在该位置。同样,按钮操作请求一个位置,并在更新到达时将地图重新居中到该位置。)我需要应用程序在应用程序运行时加载带有用户位置的地图。但是,我想让用户在需要时能够在地图上移动。一旦用户想要重新居中地图,用户将使用按钮重新居中用户位置。我希望这是清楚的。你会在发射时显示你的地图视图控制器吗?如果是这样,我描述的步骤将完全满足您的要求。(应用程序启动。它显示包含地图的视图控制器。视图控制器的
viewDidLoad()
方法启动位置管理器并请求位置。当位置传递时,地图视图控制器将地图居中于该位置。同样,按钮操作请求位置,并在更新到达时重新将地图居中于该位置。)