如何在iPhone上使用Google地图SDK获取当前位置

4
我想在当前位置上设置相机,并将缩放级别设置为10。因此,我编写了以下代码,并从这篇文章中获得了有关当前位置的提示。如何在IOS 6中使用Google地图API的代理
以下是我的代码。
mapView_=[[GMSMapView alloc]initWithFrame:CGRectZero];

CLLocationCoordinate2D currentPosition = mapView_.myLocation.coordinate;

 GMSCameraPosition* camera =
[GMSCameraPosition cameraWithTarget: currentPosition zoom: 10];
 mapView_.camera = camera;
 mapView_.delegate=self;

  mapView_.mapType = kGMSTypeSatellite;
  mapView_.myLocationEnabled = YES;

  self.view = mapView_;
  mapView_.settings.myLocationButton = YES;

但是在设备上,坐标为0.00。

请问有人能帮忙解决这个问题吗?


可能是重复的问题:关于定位自己,遇到一些问题 - Saxon Druce
3个回答

8

试试这个:

@property (nonatomic, retain) IBOutlet GMSMapView *googleMapView;
@property (nonatomic, retain) CLLocationManager *locationManager;

- (void)showCurrentLocation {
    _googleMapView.myLocationEnabled = YES;
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
            GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude 
                                                                    longitude:newLocation.coordinate.longitude 
                                                                         zoom:17.0];
            [_googleMapView animateToCameraPosition:camera];
        //...
}

这些是委托方法,应该在哪里使用? - chandru
showCurrentLocation 是我自定义的一个方法,当在我的地图视图中点击显示位置按钮时会被调用。locationManager:didUpdateToLocation:fromLocation 是 CLLocationManagerDelegate 协议方法之一:https://developer.apple.com/library/Mac/DOCUMENTATION/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html - Stunner
如何在点击位置按钮时设置animateToCameraPosition为默认操作? - Linh Nguyen

3
您可以按照以下方式为myLocation属性添加观察者:
[self.mapView addObserver:self
      forKeyPath:@"myLocation"
         options:(NSKeyValueObservingOptionNew |
                  NSKeyValueObservingOptionOld)
         context:NULL];

您应该实现以下方法:

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {

    if ([keyPath isEqualToString:@"myLocation"]) {

       NSLog(@"My position changed");
    }
}

不要忘记在您的 Info.plist 中设置以下其中一个键:NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription - hash3r

0
将这行代码写入 info.plist。
CLLocationManager requestAlwaysAuthorization (string) abcd

//代码并设置注释引脚

.h

#import<MapKit/Mapkit.h>

CLLocationManager *locationManager;
@property(nonatomic, weak) IBOutlet MKMapView* mapView;
@property(nonatomic,retain)CLLocationManager* locationManager;


- (id)init {
self = [super init];
if(self != nil) {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
}
return self;

}

在viewDidLoad中

 _mapView.showsUserLocation = YES;
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;


  if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}

[self.locationManager startUpdatingLocation];

// 从字典中获取纬度信息 NSDictionary *dictlatitude = [_dict objectForKey:@"latitude"]; // 将纬度信息转换为浮点型 float strlatitude = [[_dict objectForKey:@"latitude"] floatValue];

// 从字典中获取经度信息 NSDictionary *dictlongitude = [_dict objectForKey:@"longitude"]; // 将经度信息转换为浮点型 float strlongitude = [[_dict objectForKey:@"longitude"] floatValue];

NSLog(@"Staring Point latitude : %f", strlatitude);
NSLog(@"Staring Point longitude: %f", strlongitude);


MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = strlatitude;
location.longitude = strlongitude;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];

MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title=@"name of the pin";
ann.coordinate = region.center;
[_mapView addAnnotation:ann];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接