获取当前位置iOS,Google Maps API的坐标 [英] Getting coordinates of current location iOS, Google Maps API

查看:125
本文介绍了获取当前位置iOS,Google Maps API的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力改变Google Maps API中相机的价值。因此,只要您打开应用程序,使用CoreLocation并将这些值传递到相机,它就会显示用户的位置。
还有其他一些在线教程,但没有什么适用于我。



如果您可以提供帮助,将受到大规模赞赏。

  @implementation HMSBViewController {
CLLocationManager * _locationManager;

$ b - (NSString *)deviceLocation
{
NSString * theLocation = [NSString stringWithFormat:@latitude:%f longitude:%f,_locationManager。 location.coordinate.latitude,_locationManager.location.coordinate.longitude];
返回theLocation;
}

- (void)viewDidLoad
{
//mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; //将精度设置为尽可能最佳值


if([_ locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
}

[_locationManager startUpdatingLocation];


$ b $ - (void)loadView {

CLLocation * myLocation = mapView.myLocation;

GMSMarker * marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude,myLocation.coordinate.longitude);
marker.title = @当前位置;
marker.map = mapView;
GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude
zoom:6];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

self.view = mapView;
NSLog(@%f,%f,_locationManager.location.coordinate.latitude,_locationManager.location.coordinate.longitude);




$ b当应用程序运行时,坐标始终设置为零。

感谢

解决方案

这是因为Google Maps API只会让您当前位置放置在控制器视图上后。为了解决这个限制,你应该创建一个通知,告诉你的控制器当前位置可用。



在你的viewWillApper上添加这个观察者:

  [mapView addObserver:self forKeyPath:@myLocationoptions:0 context:nil]; 

然后添加 - (void)observeValueForKeyPath:ofObject:change:context:,每当 keyPath 值更改时调用它。在此方法中,您现在可以获取当前位置并将地图制作为动画,以便它显示在该位置。在该方法结束时,您应该注销观察者,因为如果未取消注册,则此方法将在每次更改位置时调用。

   - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {$ b $ if([keyPath isEqualToString:@myLocation]){ 
CLLocation * location = [object myLocation];
CLLocationCoordinate2D target = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude);

[mapView animateToLocation:target];

@try {
[map removeObserver:self forKeyPath:@myLocation];
} @catch(id exception){
//什么也不做,显然它没有被附加,因为引发了一个异常
}
}
}

地图可能需要一段时间才能获得当前位置,但通常很快。


I've been struggling to change the camera's value in the Google Maps API. So that it displays the users position as soon as you open the app, using CoreLocation and passing those values into the camera. There's some other tutorials online, but nothing that works for me.

If you could help it would be massively appreciated.

@implementation HMSBViewController{
    CLLocationManager *_locationManager;
}

- (NSString *)deviceLocation
{
    NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude];
    return theLocation;
}

- (void)viewDidLoad
{
    //mapView.settings.myLocationButton = YES;
    mapView.myLocationEnabled = YES;
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Setting the accuracy to the best possible


    if([_locationManager      respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [_locationManager requestAlwaysAuthorization];
    }

    [_locationManager startUpdatingLocation];

}

- (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:_locationManager.location.coordinate.latitude
                                                           longitude:_locationManager.location.coordinate.longitude
                                                             zoom:6];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    self.view = mapView;
    NSLog(@"%f, %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude);

    }

When the app runs the coordinates are always set to zero.

Thanks

解决方案

This happens because Google Maps API only get your current location after it's placed on the controller view. To solve this limitation you should create a notification that tells your controller when the current location is available.

On your viewWillApper add this observer:

[mapView addObserver:self forKeyPath:@"myLocation" options:0 context:nil];

Then add -(void)observeValueForKeyPath:ofObject:change:context: that is called everytime the keyPath value changes. On this method you can now get the current location and animate the map to it so it shows centered in there. At the end of the method you should deregister the observer since if it's not deregistered this method will be called every time the position changes.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"myLocation"]) {
        CLLocation *location = [object myLocation];
        CLLocationCoordinate2D target = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);

        [mapView animateToLocation:target];

        @try {
            [map removeObserver:self forKeyPath:@"myLocation"];
        } @catch(id exception){
            //do nothing, obviously it wasn't attached because an exception was thrown
        }
    }
}

The map can take a while to get the current location, but it's usually fast.

这篇关于获取当前位置iOS,Google Maps API的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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