如何在Mapkit的其他地方用蓝点显示用户的位置?

时间:2017-11-12 04:46:27

标签: ios swift swift3 mapkit xcode8

我是swift 3& amp;的新手。 Xcode 8,这是我第一次在这里提问。我想在Mapkit中设置几个引脚,我还想同时在同一个Mapkit中添加用户的位置。此外,我希望Mapkit的中心是一个特定的位置而不是用户的位置。但是,我遇到了一个问题。

我可以成功地在Mapkit中设置这些引脚,但是我无法在同一个Mapkit中显示用户的位置。如果我尝试将用户的位置设置为此Mapkit的中心,我可以成功显示用户的位置。我的代码如下所示。

import UIKit
import MapKit
import CoreLocation

class MapVC: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var MapView: MKMapView!

let manager = CLLocationManager()

func createSpanAndAnnotation() {
    let FirstSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.008, 0.008)
    let originalLocation:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 24.942653, longitude: 121.368598)
    let EdittedRegion:MKCoordinateRegion = MKCoordinateRegion(center: originalLocation, span: FirstSpan)
    MapView.setRegion(EdittedRegion, animated: true)

    let TemporaryGarbageCanLatitude = showData().garbageCanLatitudeArray
    let TemporaryGarbageCanLongtitude = showData().garbageCanLontitudeArray
    let TemporaryGarbageCanDiscription = showData().garbageCanDiscription
// 'showdata' is another class to store my data of garbageCans in another file.

    for i in 0...TemporaryGarbageCanLatitude.count-1 {
    let garbageCanLocation : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: TemporaryGarbageCanLatitude[i], longitude: TemporaryGarbageCanLongtitude[i])
    let pin = PinAnnotation(title: "Free Garbage Can", subtitle: TemporaryGarbageCanDiscription[i], coordinate: garbageCanLocation)
    MapView.addAnnotation(pin)
    }   
}

//-------------------------------------<Above code could work successfully>-------------------------------------
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.MapView.showsUserLocation = true
}

func locationManager2() {
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
}
//--------------------------------<It seems that Above code could not work successfully>--------------------------------

override func viewDidLoad() {
    super.viewDidLoad()
    createSpanAndAnnotation()
    locationManager2()  
 }
}

我添加了CoreLocation.framework&#39;在Build阶段,我还添加了&#39;隐私 - 使用时的位置使用说明&#39;在info.plist文件中。我在运行模拟器时也设置了位置服务。另外,我没有住在美国,所以我甚至去了模拟器&gt;调试&gt;位置&gt;自定义位置&#39;将我的位置改为我现在住的地方。

但是,在完成上述所有操作后,我仍然无法成功显示用户的位置。任何人都可以帮我解决这个问题吗?如果有人能帮助我,我将不胜感激,谢谢!

(我的Mapkit屏幕截图在这里,但我在其上模糊了一些机密信息。) simulator screen shot

1 个答案:

答案 0 :(得分:1)

我发现我通过在模拟器中更改自定义位置意外地解决了我的问题。我住在东半球,所以我住的地方的经度似乎是积极的。但是,我最初输入的经度为负值。当我将经度改为正值时,我的位置已成功显示在Mapkit上。

感谢所有帮助我解决问题的天才。

相关问题