使用MapKit获取用户位置坐标

5
我在我的应用程序中使用MapKit,并通过以下方式显示用户位置:

[mapview setShowUserLocation:YES];

我想用用户位置的坐标设置region.center.latitude和region.center.longitude,该怎么做?
5个回答

10

这是我的答案,我尝试了这样做,它可以工作:


//inside my ViewDidLOad
locManager = [[CLLocationManager alloc] init];
[locManager setDelegate:self];
[locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locManager startUpdatingLocation];

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation  {<br>
   CLLocationCoordinate2D loc = [newLocation coordinate];
   [maptView setCenterCoordiante:loc];  

}

6

更加简单:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 1500, 1500);
[mapView setRegion:region animated:YES];
}

2
不要忘记设置mapView.delegate! - DAS

2

由于某些原因,这对我没起作用。它将我带到了坐标为(0.0000,0.0000)的位置。以下是我的代码,如果您有机会检查一下,谢谢您的帮助!

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation=TRUE;

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

CLLocationCoordinate2D myCoord = {mapView.userLocation.location.coordinate.latitude,mapView.userLocation.location.coordinate.longitude};
[mapView setCenterCoordinate:myCoord animated:YES];

region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES]; 

[mapView setDelegate:self];

1

我认为这个回答了你的问题:

返回Mapkit用户位置坐标

(使用mapView.userLocation.location.coordinate.latitudemapView.userLocation.location.coordinate.longitude属性)

然后将这些坐标注入到你的MKMapView实例的

-(void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated


我可以使用 region.center.latitude = mapView.userLocation.location.coordinate.latitude; 这行代码吗?我认为它会起作用,对吗? - ludo
哼,我不确定它是否有效,我建议你创建一个本地的CLLocationCoordinate2D并将其注入到给定的选择器中。这样你就可以控制动画参数,从而让你的应用程序在动画效果上看起来更加漂亮。 - yonel
我不太知道如何使用你给我的选择器,但我会尝试,如果你有任何链接可以给我看就更好了。谢谢。 - ludo
1
CLLocationCoordinate2D myCoord = {mapView.userLocation.location.coordinate.latitude,mapView.userLocation.location.coordinate.longitude}; [mapView setCenterCoordinate:myCoord animated:YES];CLLocationCoordinate2D myCoord = {mapView.userLocation.location.coordinate.latitude,mapView.userLocation.location.coordinate.longitude}; [mapView setCenterCoordinate:myCoord animated:YES]; - yonel
我还有一个小问题,当我执行NSLog(@"%i": mapView.userLocation.location.coordinate.latitude);时,我没有位置数字,我得到的是类似于71502240这样的东西。 - ludo

0

使用委托:

  1. 添加Mapkit代理:MKMapViewDelegate
  2. 实现方法:MapView didUpdateUserLocation
    -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate,
            500, 500);
        [mapView setRegion:region animated:YES];
    }
    

没有委托:

[self.mapView setuserTrackingMode:MKUserTrackingModeFollow];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接