【发布时间】:2016-09-30 08:07:46
【问题描述】:
我在我的应用程序中使用谷歌地图。我目前在地图上获得默认位置。但我需要获取设备的当前位置并在地图上显示。 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 文件中添加一些内容?
标签: ios objective-c google-maps cllocationmanager