ios Google 地图 - 如何在不缩放到相机的情况下获取我的设备位置
ios Google Map - how to get my device location without zooming to the camera
我正在研究按钮以定期获取我的位置。在实现时,即使不使用 GMSCameraUpdate 进行对焦,设备也会定期让 Google 地图相机位于地图中心。您能否告诉我需要增强哪些功能,以便我们可以在不使用相机缩放的情况下定期获取更新的设备位置?
下面是我在onClick后触发缩放的工作
if(self.myMapView.myLocation !=nil){
[self.locationManager stopUpdatingLocation];
[CATransaction begin];
[CATransaction setAnimationDuration:0.2];
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:self.myMapView.myLocation.coordinate zoom:self.myMapView.camera.zoom];
[self.myMapView animateWithCameraUpdate:move];
[CATransaction commit];
}
获取位置后委托
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
if(location==nil){
location = self.myMapView.myLocation;
}
myDeviceLocation = location;
NSLog(@"adasdads zoom ");
if (markera == nil) {
markera = [[GMSMarker alloc] init] ;
markera.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
markera.map = self.myMapView;
}else {
[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
markera.position = location.coordinate;
markera.icon = nil;
[CATransaction commit];
}
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[self.myMapView animateWithCameraUpdate:move];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)statusX {
NSLog(@"status : %d" , statusX);
if (status == kCLAuthorizationStatusDenied) {
//location denied, handle accordingly
NSLog(@"denied ");
}
else if (statusX == kCLAuthorizationStatusAuthorized) {
//hooray! begin startTracking
NSLog(@"proceed ");
dispatch_async(dispatch_get_main_queue(), ^{
self.myMapView.myLocationEnabled = YES;
});
}else if (statusX == kCLAuthorizationStatusAuthorizedAlways || statusX == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.myMapView.myLocationEnabled = YES;
NSLog(@"gogogo ");
dispatch_async(dispatch_get_main_queue(), ^{
// self.myMapView.myLocationEnabled = YES;
});
你可以试试这个
从您的 didUpdateLocations
委托中删除此代码:
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[self.myMapView animateWithCameraUpdate:move];
将其移至您稍后可以调用的新函数。
在didUpdateLocations
中设置你的新用户位置然后调用[locationManager stopUpdatingLocation]
然后调用你的新自定义函数来设置相机。
在您的新自定义函数中,更改:
[self.myMapView animateWithCameraUpdate:move];
收件人:
CLLocationCoordinate2D target = myLocation; //2d coord
self.myMapView.camera = [GMSCameraPosition cameraWithTarget:target zoom:17];
我正在研究按钮以定期获取我的位置。在实现时,即使不使用 GMSCameraUpdate 进行对焦,设备也会定期让 Google 地图相机位于地图中心。您能否告诉我需要增强哪些功能,以便我们可以在不使用相机缩放的情况下定期获取更新的设备位置?
下面是我在onClick后触发缩放的工作
if(self.myMapView.myLocation !=nil){
[self.locationManager stopUpdatingLocation];
[CATransaction begin];
[CATransaction setAnimationDuration:0.2];
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:self.myMapView.myLocation.coordinate zoom:self.myMapView.camera.zoom];
[self.myMapView animateWithCameraUpdate:move];
[CATransaction commit];
}
获取位置后委托
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
if(location==nil){
location = self.myMapView.myLocation;
}
myDeviceLocation = location;
NSLog(@"adasdads zoom ");
if (markera == nil) {
markera = [[GMSMarker alloc] init] ;
markera.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
markera.map = self.myMapView;
}else {
[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
markera.position = location.coordinate;
markera.icon = nil;
[CATransaction commit];
}
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[self.myMapView animateWithCameraUpdate:move];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)statusX {
NSLog(@"status : %d" , statusX);
if (status == kCLAuthorizationStatusDenied) {
//location denied, handle accordingly
NSLog(@"denied ");
}
else if (statusX == kCLAuthorizationStatusAuthorized) {
//hooray! begin startTracking
NSLog(@"proceed ");
dispatch_async(dispatch_get_main_queue(), ^{
self.myMapView.myLocationEnabled = YES;
});
}else if (statusX == kCLAuthorizationStatusAuthorizedAlways || statusX == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.myMapView.myLocationEnabled = YES;
NSLog(@"gogogo ");
dispatch_async(dispatch_get_main_queue(), ^{
// self.myMapView.myLocationEnabled = YES;
});
你可以试试这个
从您的 didUpdateLocations
委托中删除此代码:
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[self.myMapView animateWithCameraUpdate:move];
将其移至您稍后可以调用的新函数。
在didUpdateLocations
中设置你的新用户位置然后调用[locationManager stopUpdatingLocation]
然后调用你的新自定义函数来设置相机。
在您的新自定义函数中,更改:
[self.myMapView animateWithCameraUpdate:move];
收件人:
CLLocationCoordinate2D target = myLocation; //2d coord
self.myMapView.camera = [GMSCameraPosition cameraWithTarget:target zoom:17];