how to show different markerInfoWindow?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 02:16:50

问题


i'm using GMS for ios. and i'm facing problem that i can't detect which marker did tapped!(Custom markerInfoWindow)

you can see my code for custom markerInfoWindow :

here i'm creating the markers :

 -(void)CreateMarks{
for (int l=0 ; l<self.NSMuatableArray.count; l++) {
    CLLocationCoordinate2D pos = CLLocationCoordinate2DMake([[[self.NSMuatableArray objectAtIndex:l] objectForKey:@"lati"] doubleValue],[[[self.NSMuatableArray objectAtIndex:l] objectForKey:@"longi"] doubleValue]);
    GMSMarker *marker = [[GMSMarker alloc]init];
    marker.position=pos;
    marker.draggable = NO;
    marker.map = mapView_;
}}

here is the delegate :

-(UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{

CustomInfoWindow*infoW = [[[NSBundle mainBundle] loadNibNamed:@"InfoWindow" owner:self options:nil] objectAtIndex:0];

for (l=0; l<self.NSMuatableArray.count; l++) {

    infoW.Title.text =[[self.NSMuatableArray objectAtIndex:l ]objectForKey:@"Title"] ;
    infoW.Time.text = [[self.NSMuatableArray objectAtIndex:l ]objectForKey:@"Time"] ;

}



return infoW;
}

so how can detect which object just tapped ?

thanks.


回答1:


Ok, your CreateMarks method is correct, the only thing missing, is some way to identify the marker afterwards. Add this to it:

marker.userData = [self.NSMuatableArray objectAtIndex:l];

Now, on your mapView:markerInfoWindow:. This loop doesn't make sense. Instead, do something like this:

-(UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{

    CustomInfoWindow*infoW = [[[NSBundle mainBundle] loadNibNamed:@"InfoWindow" owner:self options:nil] objectAtIndex:0];

    NSDictionary * data = (NSDictionary*)marker.userData;

    infoW.Title.text =[data objectForKey:@"Title"];
    infoW.Time.text = [data objectForKey:@"Time"];

    return infoW;
}

The problem is that you probably didn't understand how this method really work. It is called every time the user taps on a marker. It is actually asking What view should I show when this marker is tapped. And you were simply running over all your markers and overwriting their data on the same view.

Now, GMSMarker's have this cool property userData that can store whatever you like. It is useful to identify the marker later.



来源:https://stackoverflow.com/questions/23964224/how-to-show-different-markerinfowindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!