我正在为Mapview上的多个引脚丢弃执行此代码。
- (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;
}
这里的问题只是它只给出了最后一个引脚丢弃。我希望根据数组值进行多次引脚丢弃。
答案 0 :(得分:4)
感谢@ScarletWitch的回答......通过这样的改变,我得到了解决问题的方法。
- (void)geocode
{
NSArray *arrCityName = [NSArray arrayWithObjects:@"Baroda", @"Surat", @"Ahmedabad", @"Rajkot", @"Junagadh", @"Mumbai", @"Mehsana", @"Veraval", @"Surendranagar", @"Gondal", nil];
CLLocationCoordinate2D coordinate;
MKCoordinateRegion region;
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
[_mapView setDelegate:self];
NSMutableArray *arrPins = [[NSMutableArray alloc]init];
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];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = [arrCityName objectAtIndex:i];
ann.subtitle = @"city";
ann.imageCity = [UIImage imageNamed:@"Default-568h@2x.png"];
ann.coordinate = region.center;
[arrPins addObject:ann];
[_mapView addAnnotations:arrPins];
}
}
答案 1 :(得分:4)
在您的代码中,您只在“for”之外分配一次DisplayMap(Annotation) 循环,这是它保持最后一个值而不是添加多个的原因 注释。通过在循环中分配实例来更正代码。还有你 不需要一次又一次地设置MKCoordinateRegion。你可以设置一次 在这样的一个,它包含要显示在MapView上的所有注释。
-(void)geocode
{
NSArray *arrCityName = [NSArray arrayWithObjects:@"Baroda", @"Surat", @"Ahmedabad",
@"Rajkot", @"Junagadh", @"Mumbai", @"Mehsana", @"Veraval",
@"Surendranagar", @"Gondal", nil];
CLLocationCoordinate2D coordinate;
MKCoordinateRegion region;
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
[_mapView setDelegate:self];
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.longitudeDelta = 1.8;
region.span.latitudeDelta = 1.8;
[_mapView setRegion:region animated:YES];
for (int i =0 ; i<[arrCityName count] ; i++)
{
DisplayMap *ann = [[DisplayMap alloc] init];
coordinate = [self geoCodeUsingAddress:[arrCityName objectAtIndex:i]];
ann.title = [arrCityName objectAtIndex:i];
ann.coordinate = region.center;
[_mapView addAnnotation:ann];
}
}
答案 2 :(得分:1)
当我尝试添加多个引脚时,我遇到了类似的问题。我所做的是:设置一个全局计数器,在每个添加的注释上增加1,即在addAnnotaion上。创建for循环的另一个函数:
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];
并调用该函数,直到计数达到arrCityName的计数
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = @"CarLocation";
if ([annotation isKindOfClass:[MapViewAnnotation class]]) {
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else
{
pinView.annotation = annotation;
}
UIImageView *leftCalloutView = [[UIImageView alloc]
initWithImage:((MapViewAnnotation *)annotation).imagePin];
pinView.leftCalloutAccessoryView = leftCalloutView;
pinView.image = leftCalloutView.image;
[leftCalloutView release];
[self performSelector:@selector(intermediate) withObject:nil afterDelay:1.05];
return pinView;
}