【发布时间】:2017-02-24 13:51:51
【问题描述】:
我已经在 swift 3 中集成了谷歌地图,当地图屏幕出现而不是当前位置时,我在 .plist 文件中添加了两个键,并设置了 CLLocationManager 委托和 requestAlwaysAuthorization
class MapViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate {
@IBOutlet var mapView: GMSMapView!
var marker: GMSMarker?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "MapVC"
self.doSetupUI()
self.searchLocation()
}
override func viewWillAppear(_ animated: Bool) {
let locationManager : CLLocationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
func doGoogleMapSetup(lat : Double , lng : Double) {
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude:lng, zoom:16)
let mapView = GMSMapView.map(withFrame: .zero, camera:camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
marker.snippet = ""
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
let arrPoints : NSMutableArray = NSMutableArray()
arrPoints.add(UserDefaults.standard.object(forKey: "addressPoints"))
for i in 0..<arrPoints.count {
let path : String = (arrPoints.object(at: i)as! NSMutableArray).object(at: 0) as! String
let route : GMSPath = GMSPath.init(fromEncodedPath: path)!
let polyLine : GMSPolyline = GMSPolyline.init(path: route)
polyLine.strokeWidth = 2.0
polyLine.strokeColor = UIColor.red
polyLine.map = mapView
}
}
【问题讨论】:
-
请出示您的代码
-
另外,“位置”是什么意思?您是在说地标,指示用户位置吗?您是否正在尝试获取地图的中心坐标?还是别的什么?
-
hiii,我正在尝试获取用户的当前位置
-
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) { let latitude: Double = newLocation.coordinate.latitude let longitude: Double = newLocation.coordinate.longitude currentlat = latitude currentlong = 经度 }
-
@Dhanraj:您不应该需要地图来获取用户的位置。 locationManager.location 应该为您提供用户的地理坐标。
标签: ios swift google-maps swift3 cllocationmanager