1

我正在使用标记来显示iOS应用程序中的用户位置。我现在正在工作,但随着用户的移动,它添加了额外的标记并且不会删除旧标记。我无法使用mapview清除,因为它清除了我所有的标记,还有其他标记我不想在场景中被删除。我将附上下面的代码。谢谢谷歌地图iOS标记不删除

- (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"); 

    } 

回答

1

要删除标记,您需要保持对此方法以外的引用。如果用户的位置总是相同的标记,只需在类级别定义它,然后在你的observeValueForKeyPath方法的开始,请执行下列操作:

myMarker.map = nil; 

,而不是

myMarker = nil; 

确保将变量声明(GMSMarker *myMarker;)也移出该方法。您需要它从observeValueForKeyPath的一个呼叫持续到下一个(当用户的位置更新时)。

+0

完美!谢谢。 –

0

为什么使用标记来显示用户的位置?只需打开用户的位置并使用传统的蓝点即可。