2016-06-01 17 views
0

私は自分のアプリでGoogleマップで作業しています。私は現在マップ上のデフォルトの場所を取得します。しかし、私は地図上にそれを示すデバイスの現在の場所を取得する必要があります。 stackoverlfowにはたくさんの解決策がありますが、私の場合は何とか機能しません。これらのソリューションは、私の小さなビットコードでデフォルトのview.Lookにマップを追加する場合に動作します。Googleマップからデバイスの現在地を取得するにはどうすればよいですか?

.hファイル

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
@import GoogleMaps; 

@interface ViewController : UIViewController<CLLocationManagerDelegate, GMSMapViewDelegate> 

@property (weak, nonatomic) IBOutlet UIView *maponScreem; 
@property (nonatomic, retain) CLLocationManager *locationManager; 
@end 

.mファイル

self.locationManager.delegate = self; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
[self.locationManager startUpdatingLocation]; 

latitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude]; 
longtitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude]; 

NSLog(@"%@", latitude); 
NSLog(@"%@", longtitude); 


GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude floatValue] 
                 longitude:[longtitude floatValue] 
                  zoom:12]; 
mapView_ = [GMSMapView mapWithFrame:self.maponScreem.bounds camera:camera]; 
mapView_.delegate = self; 
mapView_.myLocationEnabled = YES; 
[self.maponScreem addSubview: self->mapView_]; 

// Creates a marker in the center of the map. 
GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake([latitude intValue], [longtitude intValue]); 
marker.title = @"Current Location"; 
marker.map = mapView_; 

私は態度と経度のための0.000000を取得します。

EDIT:
私は解決策を発見し、それがどのように動作するかこれを見て。お返事ありがとうございました。

- (void)viewDidLoad { 
[super viewDidLoad]; 



if (self.locationManager == nil) 
{ 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
} 
else 
{ 
    nil; 
} 

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{ 
    [self.locationManager requestWhenInUseAuthorization]; 
} 
else 
{ 
    nil; 
} 

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 


GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(0, 0) zoom: 16]; 
mapView_ = [GMSMapView mapWithFrame:self.maponScreem.bounds camera:camera]; 
mapView_.myLocationEnabled = YES; 
[self.maponScreem addSubview: self->mapView_]; 

} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
    { 
CLLocation *location = [locations lastObject]; 
NSString *lasti = [NSString stringWithFormat:@"%f", location.coordinate.latitude]; 
NSString *longi = [NSString stringWithFormat:@"%f", location.coordinate.longitude]; 
    // NSLog(@"%@", lat); 
// NSLog(@"%@", longi); 
    [mapView_ animateToLocation:location.coordinate]; 


} 
+0

plistファイルに何かを追加する必要があるかどうかを教えてください。 –

答えて

1

あなたのlocationManagerを初期化しておらず、また、あなたは場所にアクセスするためのユーザーの許可を求めると位置更新を開始する前に、そのdelegate.Use次のコードを設定する必要があります。

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [self.locationManager requestWhenInUseAuthorization]; //gives alert for location access 
    } 

ユーザーの所在地をお知らせします。

は、それが役立ちます:)あなたは、あなたが使用しているメソッドは推奨されません

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray<CLLocation *> *)locations 

位置情報の更新を受信する方法の下に使用する必要があります

EDIT を願っています。

+0

okこのコードは場所の取得許可を得るためにも機能しますか? –

+0

と私はviewdidloadでこのコードを配置する必要がありますか? –

+0

はい "startUpdatingLocation"を呼び出す前にviewDidLoadに追加します。 "delegUdate"を設定すると、 "didUpdateToLocation"関数のロケーション更新が提供されます。 – sanman

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

私は、これは---プロジェクト、機能タブからバックグラウンドモードを有効にして、場所のアップデートを選択し

重要である

- (IBAction)btnSetDistance:(id)sender { 

locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.pausesLocationUpdatesAutomatically = NO; 

locationManager.distanceFilter =//as per your requirment; 

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
    [locationManager requestWhenInUseAuthorization]; 
} 
[locationManager startUpdatingLocation]; 
} 

...これはあなたを助けることを願っています...

あなたはシミュレータではなくデバイス上で確認する必要があります。

関連する問題