私は自分のアプリで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];
}
plistファイルに何かを追加する必要があるかどうかを教えてください。 –