目前的位置在Google Map中无法使用 [英] Current location in not working in Google Map
问题描述
我在swift 3中整合了Google地图,当地图屏幕出现比当前位置没有显示时,我在.plist文件中添加了两个键,并设置了CLLocationManager委托和requestAlwaysAuthorization
class MapViewController:UIViewController,CLLocationManagerDelegate,UITextFieldDelegate {
@IBOutlet var mapView:GMSMapView!
var marker:GMSMarker?
$ b重写func viewDidLoad(){
super.viewDidLoad()
self.title =MapVC
self.doSetupUI()
self.searchLocation()
}
重写func viewWillAppear(_ animated:Bool){
let locationManager:CLLocationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
$ b func doGoogleMapSetup(lat:Double,lng:Double){
let camera = GMSCameraPosition .camera(withLatitude:lat,longitude:lng,zoom:16)
让mapView = GMSMapView.map(withFrame:.zero,camera:camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude:lat,longitude:lng)
marker.snippet =
marker.appearAnimation = kGMSMarkerAnimationPop
marker .map = mapView
让arrPoints: NSMutableArray = NSMutableArray()
arrPoints.add(UserDefaults.standard.object(forKey:addressPoints))
for i in 0 ..< arrPoints.count {
让路径:String =(arrPoints.object(at:i)as! NSMutableArray).object(at:0)as!字符串
let route:GMSPath = GMSPath.init(fromEncodedPath:path)!
让polyLine:GMSPolyline = GMSPolyline.init(路径:路径)
polyLine.strokeWidth = 2.0
polyLine.strokeColor = UIColor.red
polyLine.map = mapView
}
}
在 GoogleMaps
的情况下不需要任何位置管理器。我们需要的是在 .plist 中添加一个或两个键。所以确保钥匙在那里。我使用了 NSLocationWhenInUseUsageDescription
键。
< key> NSLocationWhenInUseUsageDescription<密钥>
< string>允许位置< / string>
还要确保你已经调用了 GMSServices
provideAPIKey
方法,并用 API_KEY 您在Google开发者控制台中生成的。还应该启用所有相关的Google APIs按照要求。
func应用程序(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey :任何]?) - > Bool {
//在应用程序启动后替代自定义点。
GMSServices.provideAPIKey(YOUR_API_KEY)
返回true
}
<因此,我假设您已在Google开发者控制台中完成了所有设置和事情。
通过在您的控制器中写下以下行 GoogleMap
可以显示位置允许/禁止提示并获得用户的许可。
mapView.isMyLocationEnabled = true
然而,这不会将地图设置为当前位置的动画。但是,您可以手动拖动地图来查看当前位置,并且您会在当前位置看到一个蓝点。
但是现在我们也想动画到当前位置每当我们加载 ViewController
。现在需要 CLLocationManager
到达。因此,在它的 didUpdateLocation
委托中,我们可以获取当前位置,并可以将图形设置为当前位置的动画。
import UIKit
import GoogleMaps
class ViewController:UIViewController,GMSMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet弱var mapView:GMSMapView!
var locationManager = CLLocationManager()
$ b重写func viewDidLoad(){
super.viewDidLoad()
mapView.isMyLocationEnabled = true
mapView.delegate = self
//获取当前位置的位置管理器代码
self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()
}
//位置管理器委托
func locationManager(_ manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){
let location = locations.last
let camera = GMSCameraPosition.camera(withLatitude:(location?.coordinate.latitude)!,longitude:(location?.coordinate.longitude)!, zoom:14)
mapView.animate(to:camera )
//最后停止更新位置,否则它会一次又一次地出现在这个委托中
self.locationManager.stopUpdatingLocation()
}
}
另一种做法是不使用 didUpdateLocation
,而不是使用位置管理器只需使用 GMSMapViewDelegate
委托方法 mapViewDidFinishTileRendering
func mapViewDidFinishTileRendering(_ mapView:GMSMapView){
let location = mapView.myLocation
let camera = GMSCameraPosition.camera(withLatitude:(location?.coordinate。纬度)!,经度:(位置?。坐标。经度)!,缩放:14)
mapView.animate(到:相机)
}
每次地图渲染完成时都会调用它。
但是这有一个局限性,它总会将您带到当前位置每当您使用地图进行播放时,每当您拖/捏地图/缩放地图作为渲染完成时。所以,你可以在这里实现一些 bool
变量逻辑。
你可以通过使用
let yourCurrentLocation = mapView.myLocation
确保在设备上而不是模拟器上执行此操作。如果您使用的是模拟器,则必须选择一些自定义位置,然后只有您才能看到蓝点。
。但是那是在Swift 2.x中。我在这个答案中发布的是Swift 3.x
I have integrated google map in swift 3, when map screen appear than current location in not showing, i have added two keys in .plist file and also set CLLocationManager delegate and requestAlwaysAuthorization
class MapViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate {
@IBOutlet var mapView: GMSMapView!
var marker: GMSMarker?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "MapVC"
self.doSetupUI()
self.searchLocation()
}
override func viewWillAppear(_ animated: Bool) {
let locationManager : CLLocationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
func doGoogleMapSetup(lat : Double , lng : Double) {
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude:lng, zoom:16)
let mapView = GMSMapView.map(withFrame: .zero, camera:camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
marker.snippet = ""
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
let arrPoints : NSMutableArray = NSMutableArray()
arrPoints.add(UserDefaults.standard.object(forKey: "addressPoints"))
for i in 0..<arrPoints.count {
let path : String = (arrPoints.object(at: i)as! NSMutableArray).object(at: 0) as! String
let route : GMSPath = GMSPath.init(fromEncodedPath: path)!
let polyLine : GMSPolyline = GMSPolyline.init(path: route)
polyLine.strokeWidth = 2.0
polyLine.strokeColor = UIColor.red
polyLine.map = mapView
}
}
For showing current location we don't need any location manager in case of GoogleMaps
. All we need is to add one of the keys or both in the .plist. So make sure the key is there. I have used NSLocationWhenInUseUsageDescription
key.
<key>NSLocationWhenInUseUsageDescription</key>
<string>Allow location</string>
Also make sure that you have called GMSServices
provideAPIKey
method and replaced with the API_KEY you generated in google developer console. Also all the relevant Google APIs as per requirement should be enabled.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("YOUR_API_KEY")
return true
}
So, I am assuming you have done all the settings and things right in google developer console.
By just writing the below line in your controller where you have made the GoogleMap
can show the location allow/disallow prompt and take the permission of the user.
mapView.isMyLocationEnabled = true
However this will not animate your map to your current location. But you can manually drag the map to check the current location and you will see a blue dot at your current location.
But now we also want to animate to the current location whenever we load that ViewController
. Now the need for CLLocationManager
arrives. So that in its didUpdateLocation
delegate, we can fetch the current location and can just animate the graph to the current location.
So here is my complete controller.
import UIKit
import GoogleMaps
class ViewController: UIViewController,GMSMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.isMyLocationEnabled = true
mapView.delegate = self
//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.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
mapView.animate(to: camera)
//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()
}
}
Another way of doing is not using the didUpdateLocation
and not using the location manager is just by using the GMSMapViewDelegate
delegate method mapViewDidFinishTileRendering
func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {
let location = mapView.myLocation
let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
mapView.animate(to: camera)
}
It will be called everytime the map rendering is finished.
But this comes with a limitation, it will always bring you to the current location whenever you drag/pinch/zoom map as the rendering finish everytime you play with map. So, you can just implement some kind of bool
variable logic here.
You can get your location by using
let yourCurrentLocation = mapView.myLocation
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.
I already gave this type of answer. Check this Link. But that was in Swift 2.x. The one which I posted in this answer is in Swift 3.x
这篇关于目前的位置在Google Map中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!