【发布时间】:2016-10-23 04:08:05
【问题描述】:
我需要在 Google 地图上选择当前标记位置的坐标。当标记在地图上移动时,它应该更新坐标。
我正在使用 GoogleMaps、GooglePlaces 和 GooglePlacePicker API。我可以使用 GooglePlacePicker API 获取附近的地点,但我想选择标记所在位置的精确坐标。
已经在 Uber 中完成了吗?
【问题讨论】:
标签: ios objective-c google-maps
我需要在 Google 地图上选择当前标记位置的坐标。当标记在地图上移动时,它应该更新坐标。
我正在使用 GoogleMaps、GooglePlaces 和 GooglePlacePicker API。我可以使用 GooglePlacePicker API 获取附近的地点,但我想选择标记所在位置的精确坐标。
已经在 Uber 中完成了吗?
【问题讨论】:
标签: ios objective-c google-maps
使用这个,
.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>
【讨论】:
这可以通过实现GMSMapViewDelegate 协议来完成。请参阅guide to events 和GMSMapViewDelegate 上的方法列表。
如文档中所述,
应用程序可以使用此事件来触发 GMSMapView 上显示的标记或其他内容的刷新,而不是在每次相机更改时重新加载内容。
您还可以查看Google Maps SDK for iOS 了解更多信息,了解您可以将哪些其他 API 与 Maps iOS SDK 一起使用来构建与位置相关的应用程序和网站。
【讨论】: