【问题标题】:about positioning myself,some problems关于定位自己,一些问题
【发布时间】:2013-03-07 08:11:01
【问题描述】:

我是 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。我还发现google没有回调函数引用
self.iMapView.myLocationEnabled = YES;

谢谢你的回复。

【问题讨论】:

    标签: google-maps-sdk-ios


    【解决方案1】:

    要将相机设置为动画/设置到您的当前位置,您首先必须:

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

    最好的问候

    【讨论】:

    • NSKeyValueObservingNew --> NSKeyValueObservingOptionNew
    • Google 的 API 真是太糟糕了。 KVO 已经够糟糕了。这很愚蠢。
    • 想要添加另一条评论:对于最佳 KVO 实践,请参阅 NSHipster 的优秀指南 -> nshipster.com/key-value-observing。例如,最好使用consts 和NSStringFromSelector 来实现更简洁且不易出错的实现。
    • 嗨,对于这个 googlemapview,我只想在实时运行的 mapview 上绘制 root,但是我弄错了 lat-long .. 那么我该怎么做才能获得完美的 lat-long?
    【解决方案2】:

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

    适用于 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];
    

    【讨论】:

    • 如果我使用 CLLocationManager 来定位自己。我得到了我的位置的坐标。问题是在谷歌地图视图中呈现坐标坐标。是否需要更正坐标并出现在谷歌地图视图中;
    • 但是坐标如何正确(正确的算法),并出现在谷歌地图视图中
    • 您好,我不确定我是否理解您的问题,因为我发布的更新显示了如何将地图视图移动到位置管理器提供的位置?
    • 抱歉,撒克逊人您的回答有误。请参阅下面的帖子。
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 2013-07-07
    • 2013-05-10
    • 2012-03-18
    • 2023-03-09
    • 2021-07-01
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多