1

坚持一些非常基本的东西,刚开始使用 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 上运行。我检查了所有的定位服务设置,它们似乎都打开了。

4

3 回答 3

1

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

在此处输入图像描述

于 2013-03-17T23:37:26.227 回答
1

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

MKCoordinateRegion region = {{0,0},{.5,.5}}; 
region.center.latitude = doubleLattitude;
region.center.longitude = doubleLongitude;
[mapView setRegion:region animated:YES]; 
于 2012-06-22T12:57:00.143 回答
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];

    }
     }
于 2012-04-10T04:22:50.323 回答