在Mapview上获取错误的注释位置

在Mapview上获取错误的注释位置

本文介绍了在Mapview上获取错误的注释位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mapview 上获得了错误的注释位置。我尝试了很多东西,但没有运气。
我得到了正确的坐标,但它显示在mapview上错误的位置。有谁知道为什么?

I'm getting the wrong annotation location on Mapview. I have tried so many things, but with no luck.I'm getting the right coordinate but it's displayed at the wrong location on mapview. Does anyone know why?

  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;
    }


推荐答案

我想检查你的lat- long数据类型及其值。

I think check your lat-long datatype and its value.

这篇关于在Mapview上获取错误的注释位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:10