Search code examples
iosgoogle-mapsgmsmapview

markerinfowindow for every latitude longitude google map ios


I have latitude longitude and other information I get from API but I want to dynamically show the markerinfowindow with display some information like name number

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;
         }
     }

i have array of number ,name ,lat and long inshort all information here and for markerinfowindow I am using this delegate method and for now I add static view but I want to show dynamically markerinfowindow as per array

- (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;

}

Solution

  • You can use the property userData of GMSMarker

    example: you have data like

    json = [{"id":1,"name":"demo", "city":"BOTAD"},{"id":2,"name":"demo2", "city":"SURAT"}]
    

    And if you want to show the data of BOTAD then set userData like

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

    after that you can get this under the delegate method like

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

    now it is very easy to get the data like city and name from the dictionary and you can set text on your label.