获取当前位置iOS,Google Maps API的坐标

时间:2015-05-13 10:29:07

标签: ios google-maps

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

如果你能提供帮助,那将非常感激。

@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);

    }

当app运行时,坐标始终设置为零。

由于

1 个答案:

答案 0 :(得分:1)

这是因为Google Maps API仅在将其放置在控制器视图后获取您当前的位置。要解决此限制,您应该创建一个通知,告诉您的控制器当前位置何时可用。

在viewWillApper上添加此观察者:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == annotationView.rightCalloutAccessoryView {
        println("Pressed!")
    }
}

然后添加[mapView addObserver:self forKeyPath:@"myLocation" options:0 context:nil]; 每次-(void)observeValueForKeyPath:ofObject:change:context:值更改时调用的keyPath。在这个方法上,您现在可以获取当前位置并将地图设置为动画,以便在那里显示居中。在方法结束时,您应该取消注册观察者,因为如果它没有注销,则每次位置更改时都会调用此方法。

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

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