0
我无法在Google Maps API中更改相机的值。因此,只要您打开应用程序,使用CoreLocation并将这些值传递到相机,它就会显示用户的位置。我在网上看了一些教程,但我还没有设法解决它。用户坐标objective-C Google Maps API
我试图做一个方法,观察GPS坐标已被发现,然后将它们传递给相机。但它仍然显示0,0。
@implementation HMSBViewController{
CLLocationManager *_locationManager;
}
@synthesize mapView;
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
mapView.delegate = self;
//[mapView addObserver:self forKeyPath:@"myLocation" options:0 context:nil];
//mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Setting the accuracy to the best possible
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
}
- (void)viewWillAppear{
[mapView addObserver:self forKeyPath:@"myLocation" options:0 context:nil];
}
- (void)loadView{
CLLocation *myLocation = mapView.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = @"Current Location";
marker.map = mapView;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude
zoom:1];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView;
NSLog(@"%f, %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"myLocation"]) {
CLLocation *location = [object myLocation];
CLLocationCoordinate2D target = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[mapView animateToLocation:target];
@try {
[mapView removeObserver:self forKeyPath:@"myLocation"];
} @catch(id exception){
;
}
}
}
任何建议将不胜感激!
1. loadView将在viewDidLoad之前被调用。 2.在loadView中,您正在访问mapView.myLocation _before_ mapView已创建(稍后以相同的方法)并且_before_位置管理器已启动(在viewDidLoad中)。 – Anna
[获取当前位置iOS,Google Maps API的坐标]的可能重复(http://stackoverflow.com/questions/30212190/getting-coordinates-of-current-location-ios-google-maps-api) – friedbunny