更改Google地图的选定标记或更改标记的颜色? [iOS的] [英] Change Google Maps' Selected Marker or change marker's color? [iOS]

查看:121
本文介绍了更改Google地图的选定标记或更改标记的颜色? [iOS的]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有方法来改变所选标记的颜色或图像,然后在未选中时将其更改回来。我发现使用Apple地图的Yelp会更改所选标记的颜色/图像,然后在不再选择该标记时回到原始状态,并且想知道Google Map iOS SDK是否有类似的东西或是否有人遇到过这个问题,并找到了解决办法。



我曾尝试过:

我浏览了Google的关于标记的文档(找到这里),看到他们有 marker.opacity 它改变了不透明度和 marker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]]; 改变了标记的颜色。

我尝试在 - (UIView *)中手动更改它mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker; / code>加入这一行 marker.icon = [GMSMarker markerImageWithColor:[UIColor differentColor]]; 或这一行 marker.icon = [UIImage imageNamed:@differentColorImage]; 但是当你点击标记/信息窗口时,图像/颜色保持不变。

任何人有任何想法?任何帮助。提前致谢!

解决方案

只要有人遇见并看到这一点,我通过使用我自己的方法和自己的变量解决了这个问题。我使用了两个全局变量: GMSMarker * selectedMarker BOOL isMarkerActive 。在里面mapview:markerInfoWindow 我检查标记是否活跃,如果是这意味着在这之前有一个活动,所以不要高亮那个标记。之后,我将当前标记设置为选定标记,将bool设置为true,然后突出显示该标记,如下所示。

  if(self.isMarkerActive == TRUE){
[self unhighlightMarker:self.selectedMarker];
}
self.selectedMarker = marker;
self.isMarkerActive = TRUE;
[self highlightMarker:marker];

在highlightMarker方法中,我检查发送的标记是否等于地图的选定标记

   - (void)highlightMarker:(GMSMarker *)marker {
if(self.mapView.selectedMarker isEqual:marker]) {
marker.icon = [UIImage imageNamed:@marker-selected-icon];




$ b

在unhighlightMarker方法中做同样的事情

   - (void)unhighlightMarker:(GMSMarker *)marker {
marker.icon = [UIImage imageNamed:@marker-icon ]。
}

最后,我检查地图上的水龙头,看看bool是否为真(无效)mapView:(GMSMapView *)amapView didTapAtCoordinate:(CLLocationCoordinate2D)地图的选择标记不等于零坐标{
if(self.isMarkerActive == TRUE){
if(amapView.selectedMarker!= nil){
self.isMarkerActive = FALSE;
[self unhighlightMarker:self.selectedMarker];
self.selectedMarker = nil;
amapView.selectedMarker = nil;
}
}
}

希望这可以帮助其他任何人那里。


I am wondering if there is a way to change the color or image of the selected marker and then change it back when it is not selected anymore. I see that Yelp, which uses Apple Maps, changes the color/image of the selected marker and then back to the original once that one is no longer selected and was wondering if the Google Map iOS SDK had something similar or if someone has come across this problem and found a solution.

What I have tried:

I have looked through Google's Documentation on Markers (found here) and see that they have marker.opacity which changes the opacity and marker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]]; which changes the marker's color.

I have tried to manually change it in -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker; by adding this line marker.icon = [GMSMarker markerImageWithColor: [UIColor differentColor]]; or this line marker.icon = [UIImage imageNamed:@"differentColorImage"]; but when you tap out of the marker/info-window, the image/color remains the same.

Anyone have any thoughts? Anything helps. Thanks in advance!

解决方案

Just incase anyone comes and sees this, I resolved this issues by using my own method and own variables. I used two global variables which are: GMSMarker *selectedMarker and BOOL isMarkerActive. Inside of mapview:markerInfoWindow I check if the marker is active, if it is that means there was one active before this one so go unhighlight that marker. After that I set the current marker is the selected marker, set the bool to true and then highlight that marker, as shown below.

if(self.isMarkerActive == TRUE){
    [self unhighlightMarker:self.selectedMarker];
}
self.selectedMarker = marker;
self.isMarkerActive = TRUE;
[self highlightMarker:marker];

Inside the highlightMarker method I check if the sent marker I sent is equal to the map's selected marker

-(void)highlightMarker:(GMSMarker *)marker{
    if(self.mapView.selectedMarker isEqual:marker]){
        marker.icon = [UIImage imageNamed:@"marker-selected-icon"];
    }
}

Do the same in the unhighlightMarker method

-(void)unhighlightMarker:(GMSMarker* )marker{
    marker.icon = [UIImage imageNamed:@"marker-icon"];
}

Lastly, I check for taps on the map and see if the bool is true and that the map's selected marker does not equal nil

- (void)mapView:(GMSMapView *)amapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
    if(self.isMarkerActive == TRUE){
        if(amapView.selectedMarker != nil){
            self.isMarkerActive = FALSE;
            [self unhighlightMarker:self.selectedMarker];
            self.selectedMarker = nil;
            amapView.selectedMarker = nil;
        }
    }
}

Hope this helps anyone else out there.

这篇关于更改Google地图的选定标记或更改标记的颜色? [iOS的]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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