about positioning myself,some problems

前端 未结 2 1963
生来不讨喜
生来不讨喜 2020-12-11 12:24

I am a new to google map sdk for ios.I have added a map on a view.When I enter this mapView,I want to positioning myself.So I wrote :

GMSCameraPosition *came         


        
相关标签:
2条回答
  • 2020-12-11 12:54

    NOTE: This answer is wrong, see Robert's answer.

    The Google Maps SDK for iOS doesn't have any delegate to let you know when the device's position has changed.

    You would need to use the CLLocationManager class yourself, to get the device's current position, and then update the map view.

    UPDATE:

    To update the map view from the new CLLocation provided by the location manager, you do something like this:

    GMSMapView* mapView = ...;
    CLLocation* location = ...;
    GMSCameraPosition* camera = [GMSCameraPosition 
        cameraWithLatitude: location.coordinate.latitude
        longitude: location.coordinate.longitude
        zoom: 6];
    mapView.camera = camera;
    

    Or if you want the map view to animate to the new location, then use this:

    [mapView animationToCameraPosition: camera];
    
    0 讨论(0)
  • 2020-12-11 13:06

    To animate/set the camera to your current position you first have to:

    self.googleMapsView.myLocationEnabled = YES;
    

    Then in the documentation of the GMSMapView header file you will find the following comment:

    /**
    * 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;
    

    So you can setup a key value observer in your viewWillAppear Method and then you get your location update with the Location Manager of the 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]
    }
    

    And then observe the property.

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

    Do not forget to deregister your observer in the viewWillDisappear.

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        // Implement here if the view has registered KVO
        ...
        [self.googleMapsView removeObserver:self forKeyPath:@"myLocation"];
    }
    

    Best regards

    0 讨论(0)
提交回复
热议问题