gpt4 book ai didi

ios - 如何在 ios 中使用 Google map sdk 获取当前位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:20 26 4
gpt4 key购买 nike

您好,我是 Ios 的初学者,在我的项目中,我正在使用 Google SDK 在用户沿着道路行走或驾车时获取路线

但我无法获取当前位置并在我的 x 代码控制台中显示 0.0 和 0.0(纬度和经度值)请帮助我获取当前位置

我的代码如下:-

#import "ViewController.h"

@interface ViewController ()
{
GMSMapView *_mapView;
NSMutableArray *_coordinates;
LRouteController *_routeController;
GMSPolyline *_polyline;
GMSMarker *_markerStart;
GMSMarker *_markerFinish;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_mapView.settings.myLocationButton = YES;
_mapView.myLocationEnabled = YES;
_mapView.delegate = self;

}

- (void)loadView{

CLLocation *myLocation = _mapView.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = @"Current Location";
marker.map = _mapView;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:6];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

self.view = _mapView;

NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
_polyline.map = nil;
_markerStart.map = nil;
_markerFinish.map = nil;

[_coordinates addObject:[[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]];

if ([_coordinates count] > 1)
{
[_routeController getPolylineWithLocations:_coordinates travelMode:TravelModeDriving andCompletitionBlock:^(GMSPolyline *polyline, NSError *error) {
if (error)
{
NSLog(@"%@", error);
}

else if (!polyline)
{
NSLog(@"No route");
[_coordinates removeAllObjects];
}

else
{
_markerStart.position = [[_coordinates objectAtIndex:0] coordinate];
_markerStart.map = _mapView;

_markerFinish.position = [[_coordinates lastObject] coordinate];
_markerFinish.map = _mapView;

_polyline = polyline;
_polyline.strokeWidth = 3;
_polyline.strokeColor = [UIColor blueColor];
_polyline.map = _mapView;
}
}];
}
}

@end

最佳答案

获取位置是一个异步过程。因此,您需要“等待”位置更新——在本例中是通过观察“myLocation”

观察位置

此代码仅在 _mapView 在屏幕上时有效

// 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 - 如何在 ios 中使用 Google map sdk 获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788433/

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