如何“当我打开ViewController时在Google地图上显示我的当前位置?” 在Swift中?

黄ric

我正在使用iOS(Swift)的Google Maps SDK。

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

实际上,它就像Google Maps App。当您打开Goog​​le地图时,蓝点将显示您的当前位置。您不需要第一次按下“ 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()
        } 
    }
}

有人帮忙吗?太感谢了!

拉詹(Rajan Maheshwari)

对于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()

}

运行代码时,将弹出“允许”和“不允许位置”信息。只需单击允许,您将看到您的当前位置。

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我转到源视图时,如何跳转到我在 CKEditor 4 中的当前位置?

来自分类Dev

如何使用Javascript / JQuery中的Google地图根据我当前的位置显示附近的ATM

来自分类Dev

如何在Ionic 2应用程序中自动更新Google地图上的当前位置

来自分类Dev

在Windows Phone 8中,当我在地图上的任意位置点击时,我都会检索纬度和经度

来自分类Dev

在Windows Phone 8中,当我在地图上的任意位置点击时,我都会检索纬度和经度以及特定的位置名称

来自分类Dev

应用程序启动地图活动后,如何立即在android中的地图中显示我当前的位置

来自分类Dev

Firefox:当我使用“在Google中搜索”上下文菜单打开新标签页时,如何保留在当前标签页上?

来自分类Dev

在Google地图中显示我的当前位置

来自分类Dev

如何使用Google API客户端在Google Map Android中显示我当前的位置

来自分类Dev

如何在Android中的Google地图上处理当前位置图标上的点击事件

来自分类Dev

当我在android应用中单击按钮时如何打开Google Map

来自分类Dev

如何在Xcode的地图中显示我的当前位置?

来自分类Dev

如何显示在Swift中运行的当前类和函数

来自分类Dev

ContentView中的按钮可以缩放到MapBox地图上的当前位置?

来自分类Dev

我想每30秒触发一次Google Maps API中的当前位置

来自分类Dev

在我程序的当前运行实例中打开文件?

来自分类Dev

当我在 Java 中离开 JTextField 时如何显示消息

来自分类Dev

当我在iOS 7中弹出ViewController时,需要再次显示TabBar

来自分类Dev

当我从iOS中的单独ViewController调用视图时,无法显示视图

来自分类Dev

没有打开位置不显示Android中的当前位置

来自分类Dev

如何在swift3中加密数据?当我加密数据并打印它时。它以字节显示我。我使用了 RNCryptor

来自分类Dev

Android,当打开共享位置时,我的地图在系统选择中不可见

来自分类Dev

Android,当打开共享位置时,我的地图在系统选择中不可见

来自分类Dev

如何在 Swift 3 中显示当前位置图标?

来自分类Dev

我在Xcode 5中打开了Xcode 4项目。当我打开在XC5中打开的视图时,标签和按钮没有显示,我该如何解决?

来自分类Dev

我如何参考 Isar 中的当前子目标?

来自分类Dev

我如何在 tkinter 中获取标签的当前宽度

来自分类Dev

Google地图未显示在我的应用中

来自分类Dev

Swift 2-用户的当前位置在MKPolygon中

Related 相关文章

  1. 1

    当我转到源视图时,如何跳转到我在 CKEditor 4 中的当前位置?

  2. 2

    如何使用Javascript / JQuery中的Google地图根据我当前的位置显示附近的ATM

  3. 3

    如何在Ionic 2应用程序中自动更新Google地图上的当前位置

  4. 4

    在Windows Phone 8中,当我在地图上的任意位置点击时,我都会检索纬度和经度

  5. 5

    在Windows Phone 8中,当我在地图上的任意位置点击时,我都会检索纬度和经度以及特定的位置名称

  6. 6

    应用程序启动地图活动后,如何立即在android中的地图中显示我当前的位置

  7. 7

    Firefox:当我使用“在Google中搜索”上下文菜单打开新标签页时,如何保留在当前标签页上?

  8. 8

    在Google地图中显示我的当前位置

  9. 9

    如何使用Google API客户端在Google Map Android中显示我当前的位置

  10. 10

    如何在Android中的Google地图上处理当前位置图标上的点击事件

  11. 11

    当我在android应用中单击按钮时如何打开Google Map

  12. 12

    如何在Xcode的地图中显示我的当前位置?

  13. 13

    如何显示在Swift中运行的当前类和函数

  14. 14

    ContentView中的按钮可以缩放到MapBox地图上的当前位置?

  15. 15

    我想每30秒触发一次Google Maps API中的当前位置

  16. 16

    在我程序的当前运行实例中打开文件?

  17. 17

    当我在 Java 中离开 JTextField 时如何显示消息

  18. 18

    当我在iOS 7中弹出ViewController时,需要再次显示TabBar

  19. 19

    当我从iOS中的单独ViewController调用视图时,无法显示视图

  20. 20

    没有打开位置不显示Android中的当前位置

  21. 21

    如何在swift3中加密数据?当我加密数据并打印它时。它以字节显示我。我使用了 RNCryptor

  22. 22

    Android,当打开共享位置时,我的地图在系统选择中不可见

  23. 23

    Android,当打开共享位置时,我的地图在系统选择中不可见

  24. 24

    如何在 Swift 3 中显示当前位置图标?

  25. 25

    我在Xcode 5中打开了Xcode 4项目。当我打开在XC5中打开的视图时,标签和按钮没有显示,我该如何解决?

  26. 26

    我如何参考 Isar 中的当前子目标?

  27. 27

    我如何在 tkinter 中获取标签的当前宽度

  28. 28

    Google地图未显示在我的应用中

  29. 29

    Swift 2-用户的当前位置在MKPolygon中

热门标签

归档