我是一个新的谷歌地图sdk for ios.I在视图上添加了一个地图。当我输入这个mapView时,我想定位自己。所以我写道:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
_iMapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 480) camera:camera];
self.iMapView.myLocationEnabled = YES;
self.iMapView.delegate=self;
GMSMarkerOptions *annotation = [[GMSMarkerOptions alloc] init];
annotation.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
annotation.title = @"Sydney";
annotation.snippet = @"Australia";
//annotation.infoWindowAnchor=CGPointMake(0.5, 0.5);
[self.iMapView addMarkerWithOptions:annotation];
//[self.view addSubview:self.iMapView];
self.view=self.iMapView;
但是我在坐标(33.8683,151.2086)中找到了mapView视图,我只想将mapView移动到wyposition。我也发现谷歌没有回调功能参考
self.iMapView.myLocationEnabled = YES;
谢谢你的回复。
答案 0 :(得分:21)
要将相机设置为当前位置的动画/设置,首先必须:
self.googleMapsView.myLocationEnabled = YES;
然后在GMSMapView头文件的文档中,您将找到以下注释:
/**
* If My Location is enabled, reveals where the user location dot is being
* drawn. If it is disabled, or it is enabled but no location data is available,
* this will be nil. This property is observable using KVO.
*/
@property (nonatomic, strong, readonly) CLLocation *myLocation;
因此,您可以在viewWillAppear方法中设置键值观察器,然后使用GoogleMaps SDK的位置管理器更新您的位置。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Implement here to check if already KVO is implemented.
...
[self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingNew context: nil]
}
然后观察房产。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
{
[self.googleMapsView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.googleMapsView.myLocation.coordinate.latitude
longitude:self.googleMapsView.myLocation.coordinate.longitude
zoom:self.googleMapsView.projection.zoom]];
}
}
不要忘记在viewWillDisappear中取消注册观察者。
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Implement here if the view has registered KVO
...
[self.googleMapsView removeObserver:self forKeyPath:@"myLocation"];
}
祝你好运
答案 1 :(得分:0)
注意:这个答案是错误的,请参阅Robert的回答。
适用于iOS的Google Maps SDK没有任何代表可以在设备位置发生变化时通知您。
您需要自己使用CLLocationManager
课程,以获取设备的当前位置,然后更新地图视图。
<强>更新强>
要从位置管理器提供的新CLLocation
更新地图视图,您可以执行以下操作:
GMSMapView* mapView = ...;
CLLocation* location = ...;
GMSCameraPosition* camera = [GMSCameraPosition
cameraWithLatitude: location.coordinate.latitude
longitude: location.coordinate.longitude
zoom: 6];
mapView.camera = camera;
或者,如果您希望地图视图设置为新位置的动画,请使用以下命令:
[mapView animationToCameraPosition: camera];