【问题标题】:Get GPS coordinates of moving marker on Google Maps iOS在 Google Maps iOS 上获取移动标记的 GPS 坐标
【发布时间】:2016-10-23 04:08:05
【问题描述】:

我需要在 Google 地图上选择当前标记位置的坐标。当标记在地图上移动时,它应该更新坐标。

我正在使用 GoogleMapsGooglePlacesGooglePlacePicker API。我可以使用 GooglePlacePicker API 获取附近的地点,但我想选择标记所在位置的精确坐标。

已经在 Uber 中完成了吗?

【问题讨论】:

    标签: ios objective-c google-maps


    【解决方案1】:

    使用这个,

    .h

    @interface ViewController : UIViewController <CLLocationManagerDelegate> {
    
        GMSMapView *mapView_;
        GMSMarker *marker_;
    
        float currentLatitude;
        float currentLongitude;
    
    }
    
    @property(nonatomic,retain) CLLocationManager *locationManager;
    @property (nonatomic)CLLocationCoordinate2D coordinate;
    

    .m

    - (void)viewDidLoad {
        [super viewDidLoad];
         _locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDelegate:self];
        [_locationManager setDistanceFilter:kCLDistanceFilterNone];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        if (IS_OS_8_OR_LATER) {
            if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [_locationManager requestWhenInUseAuthorization];
            }
        }
        [_locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
        NSLog(@"%@",locations);
        CLLocation *currentLoc=[locations objectAtIndex:0];
        NSLog(@"CurrentLoc : %@",currentLoc);
        _coordinate=currentLoc.coordinate;
        currentLatitude = currentLoc.coordinate.latitude;
        currentLongitude = currentLoc.coordinate.longitude;
    }
    
    
    -(void)plotMarkerForLatitude:(float)latitude andLongitude:(float)longitude {
    
        // Now create maker on current location
        if (marker_ == NULL) {
            marker_ = [[GMSMarker alloc] init];
        }
        CLLocationCoordinate2D target =
        CLLocationCoordinate2DMake(latitude, longitude);
    
        marker_.position = target;
        marker_.title = @"title";
    
        marker_.appearAnimation = kGMSMarkerAnimationPop;
        NSLog(@"%f %f",latitude,longitude);
        marker_.icon = [UIImage imageNamed:@"marker"];
        marker_.snippet = @"Address";
        marker_.map = mapView_;
    }
    

    在 Plist 中:

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Allow access to get your current location</string>
    

    【讨论】:

    • 感谢您的回复。假设我坐在孟买,当我们在 GooglePlacePicker 中移动时,我将标记移动到纽约。我要得到纽约的坐标吗?
    • 你得到结果了吗?
    • 我认为它是用来更新设备当前位置的。这不是我想要的。
    【解决方案2】:

    这可以通过实现GMSMapViewDelegate 协议来完成。请参阅guide to eventsGMSMapViewDelegate 上的方法列表。

    如文档中所述,

    应用程序可以使用此事件来触发 GMSMapView 上显示的标记或其他内容的刷新,而不是在每次相机更改时重新加载内容。

    您还可以查看Google Maps SDK for iOS 了解更多信息,了解您可以将哪些其他 API 与 Maps iOS SDK 一起使用来构建与位置相关的应用程序和网站。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      相关资源
      最近更新 更多