为什么 MKMapView 把我放到海里?

发布于 2024-09-18 09:09:52 字数 528 浏览 19 评论 0原文

我有这段代码可以在用户的​​位置设置地图:

MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2; 

CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;

MKCoordinateRegion region;
region.span=span;
region.center=location;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

现在,当我执行 mapView.showsUserLocation=YES 时,它会在正确的位置显示我的位置(在我的设备上,以及模拟器中的苹果总部)然而,上面的代码两次都将我置于非洲附近的大海中!

有什么想法吗?

I have this code to set the map at the user's location:

MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2; 

CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;

MKCoordinateRegion region;
region.span=span;
region.center=location;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

Now when I do mapView.showsUserLocation=YES it shows my location at the right place (on my device, and the apple hq in the simulator) however, both times the code above puts me in the sea somewhere near africa!

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬心 2024-09-25 09:09:52

纬度/经度对 0/0 位于英国格林威治以南的赤道上。该地点位于非洲西海岸附近的几内亚湾。如果您的 pin 放置在此处,则您的 mapView.userLocation 未设置。

可能您的代码在 MKMapKit 有机会获取其位置之前就已运行。您真正想要做的是让您的 viewController 采用 MKMapKitDelegate 协议,将地图视图的 .delegate 属性设置为 self,然后实现该方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

当地图套件找到用户时,该方法将被调用,您可以使用传递给该方法的 userLocation 值来设置地图上的区域。不过,在第一次调用之前,MapView 仍在努力定位自身,而您询问其位置还为时过早。

另外,你的regionThatFits最后是一个空操作。该方法不会就地调整区域大小,它实际上返回一个调整大小的区域,供您随意使用。因此,您希望将地图视图的区域设置为该方法返回的区域。像这样:

[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];

The latitude/longitude pair 0/0 is on the equator due south of Greenwich, England. That spot is in the Gulf of Guinea, off the West coast of Africa. If that's where your pin is getting placed, then your mapView.userLocation isn't getting set.

Probably your code here is running before MKMapKit has had a chance to get its location. What you really want to do is have your viewController adopt the MKMapKitDelegate protocol, set the .delegate property of your map view to self, and then implement the method:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

That method will get called when the map kit locates the user, and you can use the userLocation value you get passed there to set the region on the map. Until that gets called the first time, though, the MapView is still working on locating itself, and your asking about its location is premature.

Also, your regionThatFits there at the end is a no-op. That method doesn't resize the region in place, it actually returns a resized region, for you to use however you will. So you want to set your map view's region to what that method returns. Like this:

[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];
朕就是辣么酷 2024-09-25 09:09:52

我所做的就是在 info.plist 上添加以下条目:

NSLocationAlwaysUsageDescription

将此信息放入此(非常好的)教程中:
https://www.youtube.com/watch?v=Z272SMC9zuQ

What I have done was add the following entry on info.plist:

NSLocationAlwaysUsageDescription

Got this info into this (very good) tutorial:
https://www.youtube.com/watch?v=Z272SMC9zuQ

烛影斜 2024-09-25 09:09:52

你可以选择这个方法..

- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated{

CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:_mapView.region.center.latitude longitude:_mapView.region.center.longitude];
CLLocationAccuracy accuracy = _mapView.userLocation.location.horizontalAccuracy;

if (accuracy) {
    current_pickup_location = loc;
    [_currentStreetSpinner startAnimating];
    [ceo reverseGeocodeLocation:loc
              completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  //                  NSLog(@"location %@",placemark.location);
                  NSLog(@"I am currently at %@",locatedAt);
                  _currentStreetLabel.text = locatedAt;
                  static_iVar = _currentStreetLabel.text;
                  [_currentStreetSpinner stopAnimating];
                  [self listDoctorsWithSpeciality_id:currentSpeciality_id
                               withProfessionalityId:currentProfessionality_id];
              }];
}

}

You can choose this method..

- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated{

CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:_mapView.region.center.latitude longitude:_mapView.region.center.longitude];
CLLocationAccuracy accuracy = _mapView.userLocation.location.horizontalAccuracy;

if (accuracy) {
    current_pickup_location = loc;
    [_currentStreetSpinner startAnimating];
    [ceo reverseGeocodeLocation:loc
              completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  //                  NSLog(@"location %@",placemark.location);
                  NSLog(@"I am currently at %@",locatedAt);
                  _currentStreetLabel.text = locatedAt;
                  static_iVar = _currentStreetLabel.text;
                  [_currentStreetSpinner stopAnimating];
                  [self listDoctorsWithSpeciality_id:currentSpeciality_id
                               withProfessionalityId:currentProfessionality_id];
              }];
}

}

酸甜透明夹心 2024-09-25 09:09:52

我回答这个问题是因为选择的答案并没有真正帮助我,而且我遇到了完全相同的问题。 didUpdateUserLocation 不太实用,因为它会使应用程序在每次位置更改时更新。上述解决方案的另一个问题是,当地图加载时,询问位置还为时过早。那么,如何在地图加载之前询问用户位置,以免缩放时将您带到非洲呢?

您需要使用 viewDidLoad 上的位置管理器来请求位置。然后就可以设置区域了。

SWIFT 3

   //top of your class
   let locManger  = CLLocationManager()


   override func viewDidLoad()   {
        super.viewDidLoad()


         //request user location
         locManager.startUpdatingLocation()


        //this is the code that sets the region based on the location

        let span = MKCoordinateSpanMake(0.5, 0.5)

        let location = CLLocationCoordinate2DMake((locManager.location?.coordinate.latitude)!, (locManager.location?.coordinate.longitude)!)

        let region = MKCoordinateRegionMake(location, span)

        yourMap.setRegion(region, animated: false)


        //stop location updating 
        locManager.stopUpdatingLocation()
 }

I am answering this question because the answer picked did not really help me and I was having the exact same issue. didUpdateUserLocation is not very practical because it makes the app update every single time the location changes. The other issue with the above solution is that when the map loads it's still too early to ask for a location. So how do you ask for user location before the the map loads so that the zooming doesn't take you to Africa?

You need to request the location by using the location manager on the viewDidLoad. Then after that you can set the region.

SWIFT 3

   //top of your class
   let locManger  = CLLocationManager()


   override func viewDidLoad()   {
        super.viewDidLoad()


         //request user location
         locManager.startUpdatingLocation()


        //this is the code that sets the region based on the location

        let span = MKCoordinateSpanMake(0.5, 0.5)

        let location = CLLocationCoordinate2DMake((locManager.location?.coordinate.latitude)!, (locManager.location?.coordinate.longitude)!)

        let region = MKCoordinateRegionMake(location, span)

        yourMap.setRegion(region, animated: false)


        //stop location updating 
        locManager.stopUpdatingLocation()
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文