Tôi đang sử dụng bản đồ Google SDK của iOS (Swift).Làm thế nào để "Hiển thị vị trí hiện tại của tôi trên bản đồ google, khi tôi mở ViewController?" trong Swift?
Có ai biết cách "Hiển thị vị trí hiện tại của tôi trên bản đồ google, khi tôi mở ViewController" không?
Thực ra nó giống như ứng dụng Google Maps. Khi bạn mở Google Maps, điểm màu xanh lam sẽ hiển thị vị trí hiện tại của bạn. Bạn không cần nhấn "myLocationButton" trong lần đầu tiên.
Vì vậy, đây là mã:
import UIKit
import CoreLocation
import GoogleMaps
class GoogleMapsViewer: UIViewController {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(23.931735,longitude: 121.082711, zoom: 7)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
// GOOGLE MAPS SDK: BORDER
let mapInsets = UIEdgeInsets(top: 80.0, left: 0.0, bottom: 45.0, right: 0.0)
mapView.padding = mapInsets
locationManager.distanceFilter = 100
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
// GOOGLE MAPS SDK: COMPASS
mapView.settings.compassButton = true
// GOOGLE MAPS SDK: USER'S LOCATION
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
// MARK: - CLLocationManagerDelegate
extension GoogleMapsViewer: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 20, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
Bất cứ ai giúp đỡ? Cảm ơn bạn rất nhiều!
này là đủ cho SHW vị trí hiện tại // GOOGLE MAPS SDK: COM PASS mapView.settings.compassButton = true // GOOGLE MAPS SDK: ĐỊA ĐIỂM CỦA NGƯỜI DÙNG mapView.myLocationEnabled = true mapView.settings.myLocationButton = true –
Có, đủ để hiển thị vị trí hiện tại. Nhưng tôi có thể đặt camera ở vị trí hiện tại của mình không? –