1
Я получаю неправильное расположение аннотаций на Mapview
. Я пробовал так много всего, но не повезло. Я получаю правильную координату, но она отображается в неправильном месте на карте. Кто-нибудь знает, почему?Неправильное расположение аннотаций на Mapview
for(WatchEntityLocation *watchEntityLocation in watchEntityLocationList)
{
locationArray=watchEntityLocation.locationList;
NSLog(@"location count:%i",[locationArray count]);
Location *location=[locationArray objectAtIndex:0];
NSLog(@"watcher latlong:%g,%g",location.latitude,location.longitude);
CLLocationCoordinate2D coordinate= CLLocationCoordinate2DMake(location.latitude,location.longitude);
if (CLLocationCoordinate2DIsValid(coordinate)) {
NSLog(@"valid Cordinate!");
} else {
NSLog(@"Invalid Cordinate!");
}
NSLog(@"latitude is::%g" , coordinate.latitude);
NSLog(@"longitude is: :%g" , coordinate.longitude);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
NSLog(@"adjustedRegion latitude is::%g" , adjustedRegion.center.latitude);
NSLog(@"adjustedRegion longitude is: :%g" ,adjustedRegion.center.longitude);
if ((adjustedRegion.center.latitude >= -90) && (adjustedRegion.center.latitude <= 90) && (adjustedRegion.center.longitude >= -180) && (adjustedRegion.center.longitude <= 180)){
NSLog(@"valid region!");
[mapView setRegion:adjustedRegion animated:YES];
mapView.showsUserLocation = NO;
}else{
NSLog(@"Invalid region!");
}
[self addAnnotation:adjustedRegion.center tlocation:watchEntityLocation.watchEntity.name];}
-(void)addAnnotation:(CLLocationCoordinate2D)lcordinate tlocation:(NSString *)name
{
MyAnnotation *annotation =
[[MyAnnotation alloc] initWithCoordinates:lcordinate
title:name
subTitle:@""];
annotation.pinColor = MKPinAnnotationColorGreen;
[mapView addAnnotation:annotation];
[annotationArray addObject:annotation];
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *av in views) {
id <MKAnnotation> mp = [av annotation];
if (![mp isKindOfClass:[MKUserLocation class]])
{
[mv selectAnnotation:mp animated:YES];
break;
}
}
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if(annotation != map.userLocation)
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
[annView setSelected:YES];
annView.pinColor = MKPinAnnotationColorRed;
annView.calloutOffset = CGPointMake(15, 15);
return annView;
}
Сколько добавляется аннотаций? Если не слишком много, пожалуйста, покажите выход NSLog (добавьте его в вопрос). Кроме того, при вызове метода addAnnotation: tlocation: (т.е. '[self addAnnotation: ...') вы передаете 'adjustRegion.center'. Было бы более точным передать не скорректированную «координату», которую вы получили из locationList. Как уже упоминалось в предыдущем вопросе, вам не нужно вызывать setRegion перед добавлением _each и every_ annotation. – Anna
привет, я добавляю несколько аннотаций, и это ["suchi", "[email protected]", "23.0494", "72.5663", "301"], ["Jeet", "[email protected]", " «23.0494», «72.5661», «301»], [«Hitesh Vaghela», «[email protected]», «37.7858», «- 122.406», «301»]]} , и я изменился в соответствии с вашими выше предложения, но все тот же вопрос – bhoomika
, когда когда я ставил его статичным, то он дает правильное местоположение, но когда я ставлю динамически, то оно дает неправильное расположение. Я использовал тот же тип данных. – bhoomika