问题
In my app, I have done JSON parsing and I got the coordinates in the form of a dictionary, I want to use the coordinates angd plot it in the map,
I have using this
SBJsonParser *jsonParser = [SBJsonParser new];
NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
Nslog(@"%@",dict);
double la=[[dict objectForKey:@"latitude"] doubleValue];
double lo=[[dict objectForKey:@"longitude"] doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = @"Hello World";
marker.animated = YES;
marker.map = mapView;
}
it is printed as
[{"driver_id":"Tn1234sunil@gmail.com","username":"sunil","latitude":"0.000000000000000",
"longitude":"0.000000000000000"},
{"driver_id":"ma12marii@yahoo.com","username":"mari","latitude":"13.040720500000000",
"longitude":"80.243139600000000"}, {"driver_id":"45sabala@gmail.com","username":"balaji","latitude":"0.000000000000000",
"longitude":"0.000000000000000"}
Then, In my log, it is getting printed as
2014-01-04 10:55:48.121 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.121 MyTaxi[608:12e03] longitude : 0.000000
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 13.040721
2014-01-04 10:55:48.122 MyTaxi[608:12e03] longitude : 80.243140
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.123 MyTaxi[608:12e03] longitude : 0.000000
But, this doesnt works properly
Does any body have an idea how to plot these points to the google maps
回答1:
try this one this might be helpful just create a for loop to your count ,increment it ...
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[[dict valueForKey:@"latitude"] doubleValue];
double lo=[[dict valueForKey:@"longitude"] doubleValue];
NSMutableArray * latArray=[[NSMutableArray alloc]init];
NSMutableArray * longArray=[[NSMutableArray alloc]init];
[latArray addObject:[NSNumber numberWithDouble:la]];
[longArray addObject:[NSNumber numberWithDouble:lo]];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:[[latArray objectAtIndex:i]doubleValue] longitude:[[longArray objectAtIndex:i]doubleValue]];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.position = CLLocationCoordinate2DMake([[latArray objectAtIndex:i]doubleValue], [[longArray objectAtIndex:i]doubleValue]);
marker.snippet = @"Hello World";
marker.animated = YES;
marker.map = mapView;
回答2:
I think , You have to alloc Marker in for loop, right now you are creating only one marker,
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[dict valueForKey:@"latitude" doubleValue];
double lo=[dict valueForKey:@"longitude" doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = @"Hello World";
marker.animated = YES;
marker.map = mapView;
.....
}
来源:https://stackoverflow.com/questions/20902732/how-to-plot-the-markers-in-google-maps-from-a-dictionay-in-ios