如何“当我打开ViewController时,在谷歌地图上显示我的当前位置?”在斯威夫特? [英] How to "Show my current location on google maps, when I open the ViewController?" in Swift?

查看:141
本文介绍了如何“当我打开ViewController时,在谷歌地图上显示我的当前位置?”在斯威夫特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS(Swift)的谷歌地图sdk。



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



实际上它就像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()

让camera = GMSCameraPosition.cameraWithLatitude(23.931735,经度:121.082711,zoom:7)
让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
扩展名GoogleMapsViewer:CLLocationManagerDelegate {

func locationManager(manager:CLLocationManager,didChangeAuthorizationStatus status:CLAuthorizationStatus){
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView。 settings.myLocationButton = true
}
}
func locationManager(经理:CLLocationManager,didUpdateLocations位置:[CLLocation]){
if let location = locations.first {
mapView.camera = GMSCameraPosition(target:location.coordinate,zoom:20,bearing:0,viewingAngle:0)
locationManager.stopUpdatingLocation()
}
}
}

任何人都可以提供帮助吗?非常感谢你!

解决方案


对于 Swift 3.x 解决方案,请查看此



添加此密钥后,只需创建一个 CLLocationManager 变量并执行以下

  @IBOutlet弱var mapView:GMSMapView! 
var locationManager = CLLocationManager()

class YourControllerClass:UIViewController,CLLocationManagerDelegate {

//你的地图启动代码
让mapView = GMSMapView.mapWithFrame( CGRectZero,相机:相机)
self.view = mapView
self.mapView?.myLocationEnabled = true

//获取当前位置的位置管理器代码
self。 locationManager.delegate = self
self.locationManager.startUpdatingLocation()
}


//位置管理器委托
func locationManager(经理:CLLocationManager,didUpdateLocations位置:[CLLocation]){

let location = locations.last

let camera = GMSCameraPosition.cameraWithLatitude((location?.coordinate.latitude)!,经度:(位置?.coordinate.longitude)!,缩放:17.0)

self.mapView?.animateToCameraPosition(相机)

//最后停止更新位置否则会一次又一次地来在这委托
self.locationManager.stopUpdatingLocation()

}

运行代码时,弹出允许和不允许位置。只需点击允许,您就会看到当前位置。



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




I am using Google maps sdk of iOS(Swift).

Has anyone know how to "Show my current location on google maps, when I open the ViewController"?

Actually it just like Google Maps App. When you open the Google Maps, the blue spot will show your current location. You don't need press the "myLocationButton" in first time.

So this is the code:

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()
        } 
    }
}

Anyone help? Thank you so much!

解决方案

For Swift 3.x solution, please check this Answer

First all of you have to enter a key in Info.plist file NSLocationWhenInUseUsageDescription

After adding this key just make a CLLocationManager variable and do the following

@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()

}

When you run the code you will get a pop up of Allow and Don't Allow for location. Just click on Allow and you will see your current location.

Make sure to do this on a device rather than simulator. If you are using simulator, you have to choose some custom location and then only you will be able to see the blue dot.

这篇关于如何“当我打开ViewController时,在谷歌地图上显示我的当前位置?”在斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆