gpt4 book ai didi

ios - 如何在 Swift 中使用 "Show my current location on google maps, when I open the ViewController?"?

转载 作者:IT王子 更新时间:2023-10-29 05:13:52 29 4
gpt4 key购买 nike

我正在使用 iOS(Swift) 的 Google map sdk。

有谁知道如何“在我打开 ViewController 时在谷歌地图上显示我的当前位置”?

实际上它就像谷歌地图应用程序。当您打开 Google map 时,蓝点将显示您当前的位置。您不需要在第一次按“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()
}
}
}

有人帮忙吗?非常感谢!

最佳答案

For Swift 3.x solution, please check this Answer

首先你们必须在Info.plist文件中输入一个 key NSLocationWhenInUseUsageDescription

enter image description here

添加此键后,只需创建一个 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()

}

当您运行代码时,您会弹出允许和不允许定位的窗口。只需单击“允许”,您就会看到您的当前位置。

确保在设备而不是模拟器上执行此操作。如果您使用的是模拟器,则必须选择一些自定义位置,然后只有您才能看到蓝点。

enter image description here

关于ios - 如何在 Swift 中使用 "Show my current location on google maps, when I open the ViewController?"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626649/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com