使用MapKit检索UserLocation坐标

时间:2010-02-01 10:34:05

标签: iphone objective-c mapkit

我在我的应用程序中使用MapKit并使用

删除用户位置

[mapview setShowUserLocation:YES];

我想用userLocation的坐标设置region.center.latitude和region.center.longitude,怎么做?

5 个答案:

答案 0 :(得分: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  {
CLLocationCoordinate2D loc = [newLocation coordinate]; [maptView setCenterCoordiante:loc]; }

答案 1 :(得分:6)

更容易:

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

答案 2 :(得分: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];

答案 3 :(得分:1)

我认为这回答了你的问题: Returning Mapkit Userlocation Coordinates

(使用mapView.userLocation.location.coordinate.latitudemapView.userLocation.location.coordinate.longitude properties

然后将这些坐标注入

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

你的MKMapView实例。

答案 4 :(得分: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];