如何通过目标c获取当前用户的纬度/经度谷歌地图IOS

时间:2015-09-18 05:28:24

标签: objective-c google-maps-api-3 xcode6

你好,我是新手ios程序员。 我尝试了很多解决方案来获取当前位置以显示在谷歌地图上, 但它不起作用。请告诉我一步一步获取用户位置。 非常感谢。

这是我的 NearMeMap.h

 #import <UIKit/UIKit.h>
 #import <GoogleMaps/GoogleMaps.h>
 @interface NearMeMap : UIViewController<GMSMapViewDelegate>
 @property(nonatomic,retain) CLLocationManager *locationManager;

 @end

这是我的 NearMeMap.m

    #import "NearMeMap.h"
        @import GoogleMaps;

        @implementation NearMeMap{
           GMSMapView *mapView_;
           BOOL firstLocationUpdate_;
        }
        @synthesize locationManager;

        - (void)viewDidLoad
        {
          [super viewDidLoad];

          GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude zoom:17];

    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = normal;

    mapView_.settings.compassButton = YES;
    mapView_.settings.myLocationButton = YES;
    NSLog(@"User's location: %@", mapView_.myLocation);

    [mapView_ addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];

    self.view = mapView_;


    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(mapView_.myLocation.coordinate.latitude, mapView_.myLocation.coordinate.longitude);
    marker.title = @"I'm Here!!";
    marker.snippet = @"Hello";
    marker.map = mapView_;
}
          -(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [mapView_ addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]]) {
        [mapView_ animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:mapView_.myLocation.coordinate.latitude longitude:mapView_.myLocation.coordinate.longitude zoom:17]];
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [mapView_ removeObserver:self forKeyPath:@"myLocation"];
}

@end

0 个答案:

没有答案