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