Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CoreLocation在MKMapView上显示当前位置_Ios_Swift_Mkmapview_Cllocationmanager - Fatal编程技术网

Ios 使用CoreLocation在MKMapView上显示当前位置

Ios 使用CoreLocation在MKMapView上显示当前位置,ios,swift,mkmapview,cllocationmanager,Ios,Swift,Mkmapview,Cllocationmanager,我已经在swift中创建了单视图应用程序。我将MKMapView放置在main.storyboard上的viewController上。然后导入 import MapKit import CoreLocation var locationManager=CLLocationManager() let authorizationStatus=CLLocationManager.authorizationStatus() let regionRadius:Double=100

我已经在swift中创建了单视图应用程序。我将MKMapView放置在main.storyboard上的viewController上。然后导入

import MapKit
import CoreLocation

  var locationManager=CLLocationManager()
    let authorizationStatus=CLLocationManager.authorizationStatus()
    let regionRadius:Double=1000
在MapVC中。在viewDidLoad函数中

mapView.delegate=self
我对MKMapViewDelegate进行了扩展

extension MapVC :MKMapViewDelegate{
    func centerMapOnUserLocation()  {
        guard let coordinate = locationManager.location?.coordinate else {return}
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius*0.2, regionRadius*0.2)
        mapView.setRegion(coordinateRegion, animated: true)

    }
}
然后我运行了一个项目,我给了我当前位置的地图

不工作: 1-用于在用户当前位置放置pin和缩放。 我进口的

import MapKit
import CoreLocation

  var locationManager=CLLocationManager()
    let authorizationStatus=CLLocationManager.authorizationStatus()
    let regionRadius:Double=1000
在viewDidLoad函数中

      locationManager.delegate=self
 configureLocationService()
我让CLLocationManagerDelegate

extension MapVC:CLLocationManagerDelegate{
    func configureLocationService() {
        if authorizationStatus == .notDetermined{
            //app doesnt know whether it is approved or denied
            locationManager.requestAlwaysAuthorization()
            //request authorization so location is used always where app opened or not
        }
        else
        {   //is determined means :1-already apporoved 2- already denied dont need to do any thing just return
            return
        }
    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        centerMapOnUserLocation()
    }
}
不工作: 2-我在main.storybaord的viewcontroller中的MKMapView左下角放置了一个按钮,其功能是将当前用户位置放置在视图控制器的中心

   @IBAction func centerMapBtnWasPressed(_ sender: Any) {
        if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse
        {
            centerMapOnUserLocation()


}
    }
如何获取当前用户位置的pin,获取有关当前用户和位置的缩放地图,并将有关当前用户位置的地图居中

您可以从此下载项目以添加pin:

let annotation = MKPointAnnotation() // add it globally so you can remove to update location

func addPin(coord: CLLocationCoordinate2D) {
    mapView.removeAnnotation(annotation)
    let centerCoordinate = CLLocationCoordinate2D(latitude: coord.latitude, longitude:coord.longitude)
    annotation.coordinate = centerCoordinate
    annotation.title = "My Pin"
    mapView.addAnnotation(annotation)
    centerZoom(annotation)
}
要放大:

func centerZoom(myPoint: MKPointAnnotation) {
    let mapCenter = myPoint.coordinates
    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegionMake(mapCenter, span)
    mapView.region = region
}
通过添加此委托方法,您可以从位置管理器获取位置更新:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let loc: CLLocationCoordinate2D = locations.first!.coordinate
    addPin(loc)
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    addPin(userLocation.coordinate)
}
或者,您可以将MapView委托给视图控制器并使用此委托方法:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let loc: CLLocationCoordinate2D = locations.first!.coordinate
    addPin(loc)
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    addPin(userLocation.coordinate)
}

Alamofire
与显示用户位置有什么关系?什么不起作用?您是否收到错误?到目前为止,Alamofire不起作用,它只是创建xcworkspace。我已经为1-当前用户位置的引脚2-关于用户位置的缩放地图3-关于当前用户位置的按钮按下中心地图编写了代码。代码在执行时未显示这些行为-不知道我是否理解。是否要在用户位置上设置pin并将其居中?是。此外,还可以缩放用户位置。使用此代码时有三个错误:1-func addPin(CLLocationCoordinate2D:coord){//错误:使用未声明的类型“coord”。2-func centerZoom(MKPointAnnotation:myPoint){//错误:使用未声明的类型“myPoint”。3-func映射视图(uMapView:MKMapView,didUpdate userLocation:MKUserLocation){addPin(locations.first)//错误:使用未解析标识符“locations”再次出现三个错误,但位于不同的位置1-centerZoom(注释)错误:调用2中缺少参数标签“coord:”-让mapCenter=myPoint。坐标错误:使用未解析标识符“myPoint”3-addPin(位置。第一)。错误:调用中缺少参数标签“coord:”。通过此链接,您可以在MapVC.swift文件中下载出现上述错误的更新代码[我发现从CLLocation到CLLOCATIONCoordinates 2D变量的转换有一个小错误。您的代码很混乱,无论如何都无法正常工作。您不尊重委托方法。只是不要复制和粘贴代码,因为我们不是来外包您的开发人员的,我们是来帮助您解决问题的,但这项任务仍然是您需要的uld在您的问题中还声明,您正在测试中使用Swift 4。并非每个人都有环境来帮助您