markerinfowindow为每个纬度经度谷歌地图ios

时间:2016-04-08 06:12:24

标签: ios google-maps gmsmapview

我有来自API的纬度经度和其他信息,但我想动态显示markerinfowindow,并显示一些信息,如姓名

if ([response count]>0)
     {
         NSArray *array = [response valueForKey:@"response"];

         for (int i=0; i<[array count]; i++)
         {
             [arrayLat addObject:[[[response valueForKey:@"response"] objectAtIndex:i] valueForKey:@"lat"]];
             [arrLong addObject:[[[response valueForKey:@"response"] objectAtIndex:i] valueForKey:@"longi"]];
             position = CLLocationCoordinate2DMake([[arrayLat objectAtIndex:i]floatValue],[[arrLong objectAtIndex:i]floatValue]);
             GMSMarker  *marker = [GMSMarker markerWithPosition:position];
             //marker.title = [titlearr objectAtIndex:i];
             // marker.snippet = @"Subtitle";
             marker.flat=YES;
             marker.map = mapView;
             mapView.delegate = self;
             //marker.title=[[[response valueForKey:@"response"] objectAtIndex:i] valueForKey:@"firstname"];
             marker.icon = [GMSMarker markerImageWithColor:[UIColor redColor]];
             mapView.delegate = self;
         }
     }

我有数字,名称,纬度和长数组这里的所有信息 对于markerinfowindow我正在使用此委托方法,现在我添加了静态视图,但我希望按照数组动态显示markerinfowindow

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{
    UIView *myview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 170, 80)];
    myview.backgroundColor=[UIColor whiteColor];

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(10,0, 150, 28)];
    [lbl setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
    lbl.text=@"3.Gregor cleasgance";
    lbl.font = [UIFont boldSystemFontOfSize:13.0f];
    lbl.textColor=[UIColor blackColor];
    [myview addSubview:lbl];


    UILabel *lbl2=[[UILabel alloc]initWithFrame:CGRectMake(10,24, 100, 20)];
    [lbl2 setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
    lbl2.text=@"Monday 07/02";
    lbl2.textColor=[UIColor darkGrayColor];
    [myview addSubview:lbl2];

    UILabel *lbl3=[[UILabel alloc]initWithFrame:CGRectMake(120,24, 100, 20)];
    [lbl3 setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
    lbl3.text=@"14:00h";
    lbl3.textColor=[UIColor darkGrayColor];
    [myview addSubview:lbl3];

    UILabel *lbl4=[[UILabel alloc]initWithFrame:CGRectMake(0,0, 7, 80)];
    lbl4.backgroundColor=[UIColor colorWithRed:255.0/255.0 green:192.0/255.0 blue:1.0/255.0 alpha:1.0];
    [myview addSubview:lbl4];
    return myview;

}

1 个答案:

答案 0 :(得分:2)

您可以使用GMSMarker的属性userData

示例:您有

之类的数据
json = [{"id":1,"name":"demo", "city":"BOTAD"},{"id":2,"name":"demo2", "city":"SURAT"}]

如果你想显示BOTAD的数据,那么设置userData就像

GMSMarker *marker = [[GMSMarker alloc] init];
marker.userData = [json objectAtIndex:0];

之后你可以在委托方法下得到这个,比如

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{
    NSDictionary *data = marker.userData;

    return mapView;
}

现在很容易从字典中获取城市和名称等数据,您可以在标签上设置文字。