2010-02-01 148 views
5

I“M在我的应用程序中使用MapKit与获取用户位置坐标与MapKit


[mapview setShowUserLocation:YES]; 

我想设置region.center.latitude和region.center.longitude用的坐标disaply用户位置用户位置,该怎么办呢

回答

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

我觉得这个回答你的问题:? Returning Mapkit Userlocation Coordinates

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

,然后注入这些坐标到

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

您的MKMapView实例

+0

耶或我可以使用region.center.latitude = mapView.userLocation.location.coordinate.latitude;我认为它会正常工作? – ludo 2010-02-01 12:15:30

+0

哼,我不确定它是否有效,我会建议你创建一个本地CLLocationCoordinate2D并将它注入给定的选择器中。您可以控制动画参数,这也可以使您的应用看起来更好用动画。 – yonel 2010-02-01 13:26:15

+0

我真的不知道如何使用你给我的选择器,但我会尝试,如果你有任何链接,告诉我也会很好。谢谢 – ludo 2010-02-01 15:30:27

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]; 
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 2012-08-15 13:53:04

+0

这是最好的解决方案。感谢你们。 – 2012-09-18 19:02:24