我在谷歌地圖工作在我的應用程序。我現在在地圖上獲得默認位置。但我需要獲取設備的當前位置並在地圖上顯示它。 stackoverlfow上有很多解決方案,但不知何故,它不適用於我的情況。如果我在默認視圖中添加地圖,這些解決方案可以工作。查看我的一點點代碼。我將如何從谷歌地圖獲取設備的當前位置?
.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的態度和經度。
編輯:
我找到解決辦法,看看這究竟是怎麼工作的。感謝大家的回答並支持我。
- (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文件? –