1
我在Mapview
上收到了錯誤的註釋位置。我嘗試了很多東西,但沒有運氣。 我得到了正確的座標,但它顯示在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:...')時,你傳遞'adjustedRegion.center'。通過您從locationList獲得的未調整「座標」會更準確。正如前面提到的問題所述,在添加_each和every_註釋之前,不需要調用setRegion。 – 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