如何为MKMapView设置区域?

时间:2013-11-08 15:50:50

标签: ios iphone objective-c

我是iOS应用程序开发的新手,我想在我的应用程序中使用图钉在一个确切的位置使用其纬度和经度。我添加了地图,但问题是它始终以其初始位置出现,推针没有出现(它在地图上,但你需要改变位置才能看到它)。如下图所示:

  1. 初始职位
  2. Initial Position

    2.-我移动地图的位置后

    enter image description here

    我想要的是将图钉的位置显示为默认位置并使用缩放。

    这是我的代码示例:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    
    
        CLLocationCoordinate2D myCoordinate = {20.5, -7.06}; //AS an Example
    
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = myCoordinate;
    
        //Drop pin on map
        [self.mapView addAnnotation:point];
    
    //Region with Zoom
    -(void)viewWillAppear:(BOOL)animated {
    
        CLLocationCoordinate2D zoomLocation;
        zoomLocation.latitude = 20.5;
        zoomLocation.longitude= -7.6;
    
    
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    
    
        [self.mapView setRegion:viewRegion animated:YES];
    }
    

    我使用了这段代码,但它始终显示初始位置(图1)

    谢谢。

1 个答案:

答案 0 :(得分:0)

  • (无效)viewDidLoad中 { [super viewDidLoad];

    MapAnnotation *ann = [[MapAnnotation alloc] init];
    
    
    
    MKCoordinateRegion region;
    region.center.latitude = 31.504679;
    region.center.longitude = 74.247429;
    region.span.latitudeDelta = 0.01;
    region.span.longitudeDelta = 0.01;
    
    [mapView setRegion:region animated:YES];
    
    
    
    
    ann.title = @"Digital Net";
    ann.subtitle = @"Office";
    ann.coordinate = region.center;
    
    [mapView addAnnotation:ann];
    

    }

  • (MKAnnotationView *)mapView:(MKMapView *)mpView viewForAnnotation:(id)annotation {

    MKPinAnnotationView * pinView = nil;

    if(annotation!= mapView.userLocation){     static NSString * defaultID = @“myLocation”;

    pinView = (MKPinAnnotationView *)[mpView dequeueReusableAnnotationViewWithIdentifier:defaultID];
    
    if(pinView == nil) {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultID];
    
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    
    }
    

    } return pinView; }

我做过类似的事情并且工作正常