Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当前位置在谷歌地图中不工作_Ios_Swift_Google Maps_Swift3_Cllocationmanager - Fatal编程技术网

Ios 当前位置在谷歌地图中不工作

Ios 当前位置在谷歌地图中不工作,ios,swift,google-maps,swift3,cllocationmanager,Ios,Swift,Google Maps,Swift3,Cllocationmanager,我在swift 3中集成了谷歌地图,当地图屏幕显示的位置比当前位置不显示时,我在.plist文件中添加了两个键,并设置了CLLocationManager委托和requestAlwaysAuthorization class MapViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate { @IBOutlet var mapView: GMSMapView! var mar

我在swift 3中集成了谷歌地图,当地图屏幕显示的位置比当前位置不显示时,我在.plist文件中添加了两个键,并设置了CLLocationManager委托和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
    }
}
类MapViewController:UIViewController、CLLocationManagerDelegate、UITextFieldDelegate{
@IBV地图视图:GMSMapView!
var标记:GMSMarker?
重写func viewDidLoad(){
super.viewDidLoad()
self.title=“MapVC”
self.doSetupUI()
self.searchLocation()
}
覆盖函数视图将出现(uo动画:Bool){
让locationManager:CLLocationManager=CLLocationManager()
locationManager.delegate=self
locationManager.requestAlwaysAuthorization()
}
func doGoogleMapSetup(纬度:双精度,液化天然气:双精度){
让摄影机=GMSCAMERAPOSION.摄影机(纬度:纬度,经度:lng,变焦:16)
设mapView=GMSMapView.map(带帧:.0,相机:相机)
mapView.isMyLocationEnabled=true
let marker=GMSMarker()
marker.position=CLLocationCoordinate2D(纬度:纬度,经度:lng)
marker.snippet=“”
marker.appearAnimation=kGMSMarkerAnimationPop
marker.map=mapView
让arrPoints:NSMutableArray=NSMutableArray()
添加(UserDefaults.standard.object(forKey:“addressPoints”))

对于0中的i…这有点详细,所以我想留下一个完整的答案。这是自几年前了解基本情况以来,我遇到的零位置最常见的原因。因此,您可以调用CLLocationManager.starting(),然后调用设置映射的方法。由于CLLocationManager在代码的另一部分中设置权限和访问用户位置所需的时间所导致的竞争条件,有时有效,有时无效t不起作用:

1) 你称之为请求总是令人震惊的 2) 在一个线程上触发用户权限设置 3) 在ViewController中,您可以请求用户的位置来设置地图 4) 结果是零 5) 现在,第2步完成,应用程序可以访问用户的位置,但为时已晚

核心问题是,从请求权限和位置开始的过程需要几毫秒以上的时间。如果您的视图已经设置好,则它需要几毫秒才能通过viewDidLoad中的方法。当您拥有所需的位置时,您已经请求了它。这导致我使用了太多的c皮疹,在我基于位置的应用程序中

我的解决方法是创建一个singleton CLLocationManager,使我的起始视图成为委托,并在该视图中请求始终授权和惊人的定位。这样,当我到达需要位置的视图时,应用程序已经开始定位,并且locationManager.location不是零


这显然不是一种适用于所有应用程序的方法。如果你需要澄清,请告诉我,如果你还需要代码。我有一些公共iO git Repo,其中有一些项目我遇到并解决了这个问题。

对于显示当前位置,我们不需要任何位置管理器来处理
谷歌地图的问题。
eed将在.plist中添加一个或两个键。因此,请确保键在那里。我在使用USAGESCRIPTION
键时使用了
nslocation

<key>NSLocationWhenInUseUsageDescription</key>
    <string>Allow location</string>
所以,我假设您已经在google开发者控制台中正确地完成了所有设置和操作

只需在控制器中编写下一行,您就可以在其中创建
GoogleMap
显示位置允许/禁止提示,并获得用户的许可

mapView.isMyLocationEnabled = true
但是,这不会将地图动画化到当前位置。但是,您可以手动拖动地图以检查当前位置,您将在当前位置看到一个蓝点

但是现在我们还想在加载
ViewController
时将动画设置到当前位置。现在需要
CLLocationManager
了。这样,在它的
didUpdateLocation
委托中,我们可以获取当前位置,并可以将图形设置到当前位置

这是我的完整控制器

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

    }
}
另一种方法是不使用
didUpdateLocation
,也不使用位置管理器,只需使用
GMSMapViewDelegate
delegate方法
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)
}
每次完成地图渲染时都会调用它。
但这有一个限制,每当你拖动/收缩/缩放贴图时,它总是会将你带到当前位置,因为每次你玩贴图时渲染都会完成。因此,你可以在这里实现某种
bool
变量逻辑

您可以使用

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


我已经给出了这种类型的答案。请检查这个。但那是在Swift 2.x中。我在这个答案中发布的答案是在Swift 3.x中

请同时显示您的代码,您所说的“位置”是什么意思?你说的是一个指示用户位置的地标吗?你是想得到地图的中心坐标吗?还是别的什么?嗨,我想得到用户的当前位置Func locationManager(manager:CLLocationManager,didUpdateToLocation newLocation:CLLocation,fromLocation oldLocation:CLLocation){let latitude:Double=newLocation.coordinate.latitude let longitude:Double=newLocation.coordinate.longitude currentlat=latitude currentlong=longitude}@Dhanraj:你不应该需要地图来获取用户的位置。locationManager.location应该为你提供用户的地理坐标。Hiii dylantheli
let yourCurrentLocation = mapView.myLocation