Google 地图 iOS 未删除标记
Google Maps iOS Marker Not Deleting
我正在使用标记在我的 iOS 应用程序中显示用户位置。我现在让它工作,但随着用户移动,它会添加额外的标记并且不会删除旧标记。我不能使用 mapview clear,因为它会清除我所有的标记,并且场景中还有其他我不想删除的标记。我将在下面附上代码。谢谢
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapIcon = @"largemapicon";
GMSMarker *myMarker;
CLLocationCoordinate2D coordi=location.coordinate;
myMarker = nil;
myMarker=[GMSMarker markerWithPosition:coordi];
// marker.snippet = coordinates[@"name"];
myMarker.map = self.mapView;
myMarker.appearAnimation = kGMSMarkerAnimationPop;
UIImage * image = [UIImage imageNamed:mapIcon];
CGSize sacleSize = CGSizeMake(45, 45);
UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myMarker.icon = resizedImage;
NSLog(@"movement");
}
要删除标记,您需要在此方法之外保留对它的引用。如果用户的位置始终是相同的标记,只需在 class 级别定义它,然后在 observeValueForKeyPath
方法的开头,执行以下操作:
myMarker.map = nil;
而不是
myMarker = nil;
确保将您的变量声明 (GMSMarker *myMarker;
) 也移出该方法。您需要它从 observeValueForKeyPath
的一次调用持续到下一次调用(当用户的位置更新时)。
为什么要使用标记来显示用户的位置?只需开启用户定位,使用传统蓝点即可。
我正在使用标记在我的 iOS 应用程序中显示用户位置。我现在让它工作,但随着用户移动,它会添加额外的标记并且不会删除旧标记。我不能使用 mapview clear,因为它会清除我所有的标记,并且场景中还有其他我不想删除的标记。我将在下面附上代码。谢谢
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapIcon = @"largemapicon";
GMSMarker *myMarker;
CLLocationCoordinate2D coordi=location.coordinate;
myMarker = nil;
myMarker=[GMSMarker markerWithPosition:coordi];
// marker.snippet = coordinates[@"name"];
myMarker.map = self.mapView;
myMarker.appearAnimation = kGMSMarkerAnimationPop;
UIImage * image = [UIImage imageNamed:mapIcon];
CGSize sacleSize = CGSizeMake(45, 45);
UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myMarker.icon = resizedImage;
NSLog(@"movement");
}
要删除标记,您需要在此方法之外保留对它的引用。如果用户的位置始终是相同的标记,只需在 class 级别定义它,然后在 observeValueForKeyPath
方法的开头,执行以下操作:
myMarker.map = nil;
而不是
myMarker = nil;
确保将您的变量声明 (GMSMarker *myMarker;
) 也移出该方法。您需要它从 observeValueForKeyPath
的一次调用持续到下一次调用(当用户的位置更新时)。
为什么要使用标记来显示用户的位置?只需开启用户定位,使用传统蓝点即可。