1

我是 google map sdk for ios 的新手。我在视图上添加了一张地图。当我进入这个 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;

谢谢你的回复。

4

2 回答 2

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"];
}

此致

于 2013-03-08T10:02:30.917 回答
0

注意:这个答案是错误的,请参阅罗伯特的答案。

适用于 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];
于 2013-03-07T08:18:40.307 回答