簡體   English   中英

如何在iPhone中獲取Google Map sdk中的當前位置

[英]how to get current location in google map sdk in iphone

我想將攝像頭設置在當前位置,並將縮放級別設置為10。因此,我已經編寫了這樣的代碼,對於當前位置,我從這篇文章中得到了提示。

如何在IOS 6的Google Map 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。

請幫助可以解決這個問題嗎?

嘗試這個:

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

您可以為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");
    }
}

在信息列表中寫這行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM