MKMapView - 中心位置没有更新,让我在海洋中间?

时间:2012-04-07 01:17:23

标签: mkmapview

坚持一些非常基本的东西,刚开始使用MKMapView并尝试让它放大我的位置。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate; }

这段代码似乎没有用,我明白通常需要一点时间来获取位置,我已经离开了一段时间,现在运气好了。然而,这个位置显示正确,但它没有以此为中心(它位于西非非洲地图的中间位置)

我在viewDidLoad中也有以下代码,它似乎是正确的,因为它放大到指定的高度,而不是放置。

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 20000, 20000);
[self.mapView setRegion:region animated:YES];

如果我在这里做了一些非常愚蠢的事情,请跟我一起露面,我才刚开始!

提前致谢。

编辑:现在在按下一次或两次然后重新查看地图后,现在正在模拟器上工作。但它仍然无法在我的iPhone上运行...我检查了所有的位置服务设置,它们似乎全部开启。

3 个答案:

答案 0 :(得分:1)

我使用以下代码设置要在MapView中显示的中心和跨度。它应该工作。

MKCoordinateRegion region = {{0,0},{.5,.5}}; 
region.center.latitude = doubleLattitude;
region.center.longitude = doubleLongitude;
[mapView setRegion:region animated:YES]; 

答案 1 :(得分:1)

如果MKMapView位于加蓬以西,那么你的一个物体很可能是零,因此坐标是0°纬度和0°经度。您可以通过在调试器中设置断点并分析对象来轻松测试它。

enter image description here

答案 2 :(得分:0)

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate; 

不会将地图置于用户位置的中心位置。  只有在设备获取用户位置时才会调用- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation,  更好的使用 - (void)locationManager:didUpdateToLocation: fromLocation:

- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation {



    if(newLocation.horizontalAccuracy>0)
    {
    // set center of map (span will not be changed in this case)
    [mymap setCenterCoordinate:newLocation.coordinate animated:YES];

    // or use region to specify both span and center
    MKCoordinateRegion region.span.latitudeDelta = .05 ;
         region.span.longitudeDelta = .05 ;
         region.center.latitude = newLocation.coordinate.latitude ;
         region.center.longitude = newLocation.coordinate.longitude ;
            [mymap setRegion:region animated:TRUE];

    }
     }