我正在为 Mapview 上的多个 pin drop 执行此代码。
- (void)geocode
{
NSArray *arrCityName = [NSArray arrayWithObjects:@"Baroda", @"Surat", @"Ahmedabad", @"Rajkot", @"Junagadh", @"Mumbai", @"Mehsana", @"Veraval", @"Surendranagar", @"Gondal", nil];
CLLocationCoordinate2D coordinate;
MKCoordinateRegion region;
DisplayMap *ann = [[DisplayMap alloc] init];
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
[_mapView setDelegate:self];
for (int i = 0; i < [arrCityName count]; i++)
{
coordinate = [self geoCodeUsingAddress:[arrCityName objectAtIndex:i]];
NSLog(@"Lat: %f --- Long: %f --- for city:--> %@",coordinate.latitude,coordinate.longitude,[arrCityName objectAtIndex:i]);
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.longitudeDelta = 1.8;
region.span.latitudeDelta = 1.8;
[_mapView setRegion:region animated:YES];
ann.title = [arrCityName objectAtIndex:i];
ann.coordinate = region.center;
[_mapView addAnnotation:ann];
}
}
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
这里的问题只是它只给出最后一个针脚。我想根据数组值进行多个引脚放置。