【发布时间】:2013-11-21 13:54:23
【问题描述】:
我在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 获得的未调整的coordinate会更准确。如上一个问题所述,您不需要在添加 each and every 注释之前调用 setRegion。 -
嗨,我正在添加多个注释,即 ["suchi","suchi@gmail.com","23.0494","72.5663","301"],["Jeet","jeet @gmail.com","23.0494","72.5661","301"],["Hitesh Vaghela","hiteshvaghela116@yahoo.com","37.7858","-122.406","301"]]} 和我已根据您的上述建议进行了更改,但仍然是相同的问题
-
当我将其放置为静态时,它会给出正确的位置,但当我动态放置时,它会给出错误的位置。我使用了相同的数据类型。
-
前两个注释在印度,第三个在旧金山。它们出现在地图的什么位置?我要求实际 NSLog 输出的原因是查看代码实际读取和执行的操作(而不是您认为应该执行的操作),但您显示的是原始原始数据。
标签: iphone objective-c ios7