【问题标题】:how to get current location in google map sdk in iphone如何在 iphone 的 google map sdk 中获取当前位置
【发布时间】:2013-05-02 06:32:21
【问题描述】:

我想将相机设置在当前位置并将缩放级别设置为 10。 所以我写了这样的代码,对于当前位置,我从这篇文章中得到了提示。

How to use delegates in Google map API for IOS 6

这是我的代码

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。

请大家帮忙解决这个问题?

【问题讨论】:

标签: iphone google-maps google-maps-sdk-ios


【解决方案1】:

试试这个:

@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];
        //...
}

【讨论】:

  • 这些是委托方法吗,在哪里用这个
  • showCurrentLocation 是我的一种自定义方法,当在我的地图视图中点击显示位置按钮时会调用该方法。 locationManager:didUpdateToLocation:fromLocation是CLLocationManagerDelegate的一个协议方法:developer.apple.com/library/Mac/DOCUMENTATION/CoreLocation/…
  • 如何设置 animateToCameraPosition 是点击位置按钮时的默认动作?
【解决方案2】:

您可以为 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 中设置以下键之一:NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription
【解决方案3】:

在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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多