如何显示pined location不是用户在swift 3中的位置?

时间:2017-05-01 07:09:00

标签: ios swift3 annotations location mapkit

我的地图中有一个特殊的位置,经纬度经度我的问题是两件事: 1-我希望在我打开应用程序时看到非常有用的位置

2-我希望用户可以缩小并可以看到他的位置(请记住,重要的是我希望用户在应用运行时可以轻松找到特殊位置)

这是我的代码

@IBOutlet var map : MKMapView!
let manager = CLLocationManager()
var locationsArray = [CLLocation]()


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations[0]
    let span : MKCoordinateSpan = MKCoordinateSpanMake(35.755515 , 51.523357)
    let mylocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region : MKCoordinateRegion = MKCoordinateRegionMake(mylocation, span)
    map.setRegion(region, animated: true)

    self.map.showsUserLocation = true

}

let locations = [["title": "تیپو", "latitude": 35.7722206, "longitude": 51.3806899 ],]


override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.startUpdatingLocation()


    for location in locations {
        let annotation = MKPointAnnotation()
        annotation.title = location["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
        map.addAnnotation(annotation)
        manager.stopUpdatingLocation()
        self.map.delegate = self

        let request = MKDirectionsRequest()
        request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 35.7722206, longitude: 51.3806899), addressDictionary: nil))
        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 35.7722206, longitude: 51.3806899), addressDictionary: nil))
        request.requestsAlternateRoutes = false
        request.transportType = .walking

        let directions = MKDirections(request: request)

        directions.calculate { [unowned self] response, error in
            guard let unwrappedResponse = response else { return }

            for route in unwrappedResponse.routes {
                self.map.add(route.polyline)
                self.map.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
            }
        }
    }


    self.tabBarController?.tabBar.isHidden = false
    self.navigationController?.isNavigationBarHidden = true

    Manager.requestWhenInUseAuthorization();
    if CLLocationManager.locationServicesEnabled() {
        Manager.delegate = self
        Manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        Manager.startUpdatingLocation()

    }
    else{
        print("Location service disabled");
    }
}


var Manager = CLLocationManager()
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
}

我在Info.Plist中添加了必要的东西,并将MapKit和CoreLocation以及Delegate方法导入到我的班级

0 个答案:

没有答案