gpt4 book ai didi

ios - GMSMapView myLocation 未提供实际位置

转载 作者:可可西里 更新时间:2023-11-01 03:29:38 30 4
gpt4 key购买 nike

我有一个 GMSMapView 正确加载并在我的 View Controller 中工作

我无法做的是围绕我的位置设置 GMSCameraPosition

这是我的代码:

mapView_.myLocationEnabled = YES;
CLLocation* myLoc = [mapView_ myLocation];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLoc.coordinate.latitude
longitude:myLoc.coordinate.longitude
zoom:4];
[mapView_ setCamera:camera];

GPS 已启用并且应用程序具有所有需要的权限,但 myLocation 返回 nil CLLocation,因此 cameraWithLatitude:longitude:zoom: 获取 0 0 坐标并显示非洲而不是我的实际位置(不在非洲 :) )

最佳答案

来自官方 Google Maps iOS SDK 文档:

  • (BOOL) myLocationEnabled [read, write, assign] Controls whether the My Location dot and accuracy circle is enabled.

Defaults to NO.

  • (CLLocation*) myLocation [read, assign] If My Location is enabled, reveals where the user location dot is being drawn.

If it is disabled, or it is enabled but no location data is available, this will be nil. This property is observable using KVO.

因此,当您设置 mapView_.myLocationEnabled = YES; 时,它只会告诉 mapView 只有在您为 指定位置值时才显示蓝点>我的位置 属性。 Google 的示例代码显示了如何使用 KVO 方法观察用户位置。(推荐)您还可以实现 CLLocationManagerDelegate 方法,以更新 mapView。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[mapView animateToLocation:newLocation.coordinate];
// some code...
}

以下是来自 google maps 示例代码的代码,介绍如何使用 KVO 更新用户位置。

// in viewDidLoad method...
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}

关于ios - GMSMapView myLocation 未提供实际位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17366403/

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