Eri*_*ang 19 google-maps ios swift
我正在使用iOS(Swift)的谷歌地图sdk.
有谁知道如何"当我打开ViewController时,在谷歌地图上显示我的当前位置"?
实际上它就像谷歌地图应用程序.当您打开Google地图时,蓝点将显示您当前的位置.您不需要在第一次按"myLocationButton".
所以这是代码:
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人帮吗?非常感谢!
Raj*_*ari 38
对于Swift 3.x解决方案,请查看此答案
首先,所有人都必须在Info.plist文件中输入密钥
NSLocationWhenInUseUsageDescription
添加此密钥后,只需创建一个CLLocationManager
变量并执行以下操作
@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
class YourControllerClass: UIViewController,CLLocationManagerDelegate {
//Your map initiation code
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.view = mapView
self.mapView?.myLocationEnabled = true
//Location Manager code to fetch current location
self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()
}
//Location Manager delegates
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let camera = GMSCameraPosition.cameraWithLatitude((location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)
self.mapView?.animateToCameraPosition(camera)
//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()
}
Run Code Online (Sandbox Code Playgroud)
运行代码时,弹出"允许"和"不允许位置".只需点击"允许",您就会看到当前位置.
确保在设备而不是模拟器上执行此操作.如果您使用的是模拟器,则必须选择一些自定义位置,然后才能看到蓝点.
Iyy*_*avi 11
使用此代码,
你错过了addObserver
方法和一些内容,
viewDidLoad
:
mapView.settings.compassButton = YES;
mapView.settings.myLocationButton = YES;
mapView.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil)
dispatch_async(dispatch_get_main_queue(), ^{
mapView.myLocationEnabled = YES;
});
Run Code Online (Sandbox Code Playgroud)
观察者方法:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if change[NSKeyValueChangeOldKey] == nil {
let location = change[NSKeyValueChangeNewKey] as CLLocation
gmsMap.camera = GMSCameraPosition.cameraWithTarget(location.coordinate, zoom: 16)
}
}
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助
小智 7
首先将以下内容添加到您的 info.plist
第二个去https://developers.google.com/maps/documentation/ios-sdk/start并按照步骤直到第 5 步
设置完所有内容后要做的最后一件事是转到您的 ViewController 并粘贴以下内容
import UIKit
import GoogleMaps
class ViewController: UIViewController,CLLocationManagerDelegate {
//Outlets
@IBOutlet var MapView: GMSMapView!
//Variables
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
initializeTheLocationManager()
self.MapView.isMyLocationEnabled = true
}
func initializeTheLocationManager() {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var location = locationManager.location?.coordinate
cameraMoveToLocation(toLocation: location)
}
func cameraMoveToLocation(toLocation: CLLocationCoordinate2D?) {
if toLocation != nil {
MapView.camera = GMSCameraPosition.camera(withTarget: toLocation!, zoom: 15)
}
}
}
Run Code Online (Sandbox Code Playgroud)(不要忘记在故事板中添加一个视图并将其连接到 MapViw)
现在您可以构建并运行以在 google 地图上查看您当前的位置,就像打开 Google Map App 时一样
享受编码:)
Swift 3.0 或更高版本
要在 GMS 地图视图中显示用户位置(蓝色标记),请确保您拥有位置权限并添加此行
mapView.isMyLocationEnabled = true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61037 次 |
最近记录: |