首页 > 解决方案 > 停止更新地图查看位置

问题描述

加载后如何停止更新用户的位置。就像阻止用户位置引脚继续移动和更新一样。我仍然想要那里的位置图标,但要停止更新。已经试过了

locationmanager.delegate = nil locationManager.stopMonitoringSignificantLocationChanges()

标签: iosswiftxcode

解决方案


import UIKit
import GoogleMaps

class Map: UIViewController, CLLocationManagerDelegate  
{

var mapView: GMSMapView?
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 40.7, longitude: -74.0, zoom: 4.0))
    view = mapView

    if (CLLocationManager.locationServicesEnabled()){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let center = CLLocationCoordinate2D(latitude: locations.last!.coordinate.latitude, longitude: locations.last!.coordinate.longitude)

    let camera = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 4.0);
    self.mapView?.camera = camera
    self.mapView?.isMyLocationEnabled = true

    let marker = GMSMarker(position: center)

    print("Latitude :\(center.latitude)")
    print("Longitude :\(center.longitude)")
    marker.title = "Current Location"

    locationManager.stopUpdatingLocation()
}

}

在这种情况下,如果您想在您的位置之后停止更新位置,那么您需要编写:-

locationManager.stopUpdatingLocation()

推荐阅读