【问题标题】:Google Maps iOS Marker Not Deleting谷歌地图 iOS 标记未删除
【发布时间】:2016-06-21 12:20:33
【问题描述】:

我正在使用标记在我的 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");

    }

【问题讨论】:

    标签: ios objective-c google-maps google-maps-markers google-maps-sdk-ios


    【解决方案1】:

    要删除标记,您需要在此方法之外保留对它的引用。如果用户的位置始终是同一个标记,只需在类级别定义它,然后在 observeValueForKeyPath 方法的开头,执行以下操作:

    myMarker.map  = nil;
    

    而不是

    myMarker = nil;
    

    请务必将您的变量声明 (GMSMarker *myMarker;) 也移出该方法。您需要它从observeValueForKeyPath 的一次调用持续到下一次(当用户的位置更新时)。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-10
        • 2012-10-17
        • 2018-10-13
        • 1970-01-01
        • 2017-02-10
        • 2014-05-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多