如何在iOS Google Maps中保留标记的参考 [英] How to keep a reference of marker in iOS Google Maps

查看:57
本文介绍了如何在iOS Google Maps中保留标记的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,根据文档,我需要保留一份参考到标记.现在,在我的viewWillDisappear方法中,我试图清除地图上的其中一个图钉,但它不起作用.也许是因为我没有提及它?如果是这样,那该如何运作? (顺便说一句,我不能让它也可以用作其他任何方法的一部分,而不仅仅是viewWillDisappear.)

So according to the documentation, I need to keep a reference to the marker. Right now, in my viewWillDisappear method I'm trying to clear one of the pins off the map but it's not working. Maybe it's because I don't have a reference to it? If so, how does that work? (By the way, I couldn't get it to work as a part of any other method as well, not just viewWillDisappear.)

我具有标记的纬度和经度,这就是我现在正在做的事情:

I have the latitude and longitude of the marker and here's what I'm doing now:

CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[passedCoordinates objectAtIndex:0]doubleValue], [[passedCoordinates objectAtIndex:1]doubleValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = nil;

但这还没有清除.有什么建议吗?

But it's not clearing. Any advice?

我现在正在保留参考,这就是我试图查找要清除的标记的方法:

I'm keeping a reference now and this is how I'm trying to find which marker to clear:

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    if (passedCoordinates.count != 0) {
        for (GMSMarker *markerRef in _markerRefs){
            if (markerRef.position.latitude == [[passedCoordinates objectAtIndex:0]doubleValue] && markerRef.position.longitude == [[passedCoordinates objectAtIndex:1] doubleValue]) {
                NSLog(@"here");
                markerRef.map = nil;
            }
        }
    }
}

我得到的日志输出是它找到了正确的标记,但是并没有消失.我在导航栏上添加了一个按钮,只是为了单击并删除具有相同功能的标记,但该标记仍在地图上.

I get the log output that it's finding the correct marker but it's not going away. I added a button on the navigation bar just to click and remove the marker with the same function but it's still on the map.

推荐答案

调用markerWithPosition:时,将创建具有给定位置的新对象.它不会返回该位置的旧标记对象的指针. 创建标记时,应将其保存在数组中:

When you call markerWithPosition: its creating a new object with given position. It will not return your old marker object's pointer with that position. When you creating your markers, you should keep it in an array:


@interface YourClass()

// Declaring array that will hold all markers
@property (strong, nonatomic) NSMutableArray *allMarkers;

//...
@end

@implementation

//...
- (void)yourMethod
{
     if (!self.allMarkers) {
         self.allMarkers = [[NSMutableArray alloc] init];
     }

   // Here, when you creating your markers and adding to self.mapView
      CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[passedCoordinates objectAtIndex:0]doubleValue], [[passedCoordinates objectAtIndex:1]doubleValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
      marker.map  = self.mapView;

   // add allocated markers to allMarkers array
     [self.allMarkers addObject:marker]
}

//...
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    for (GMSMarker *marker in self.allMarkers) {
        // Remove marker from the map
        marker.map = nil;
    }
}

这篇关于如何在iOS Google Maps中保留标记的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆