[英]How to move marker on gmsmapview in google sdk ios
我想基於lat,long來移動標記,而我正在不斷地像下面這樣進行操作...
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations (NSArray<CLLocation *> *)locations
{
CLLocationCoordinate2D coor2D = CLLocationCoordinate2DMake(longitude, latitude);
self.marker.position = coor2D;
}
假設您已設置委托,請嘗試以下操作:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations (NSArray<CLLocation *> *)locations
{
CLLocationCoordinate2D coor2D = CLLocationCoordinate2DMake(longitude, latitude);
self.marker.position = coor2D;
[self.mapView animateToLocation:coord2D.coordinate]
}
如果您要做的只是更新您的當前位置。 您可能應該只使用KVO。
@implementation MyLocationViewController {
GMSMapView *mapView_;
BOOL firstLocationUpdate_;
}
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView_;
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
- (void)dealloc {
[mapView_ removeObserver:self
forKeyPath:@"myLocation"
context:NULL];
}
#pragma mark - KVO updates
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.