我如何动态设置MKMapView的区域跨度,以使其不会“捕捉"到屏幕上.重绘地图后又回到初始区域? [英] How can I set MKMapView's region span dynamically so that it doesn't "snap" back to the initial Region upon redrawing the map?

查看:102
本文介绍了我如何动态设置MKMapView的区域跨度,以使其不会“捕捉"到屏幕上.重绘地图后又回到初始区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MKMapview,但缩放级别和区域范围存在问题.

I am working with a MKMapview and I am having a problem with the zoom level and the region span.

当刷新MKMapView时,似乎将区域重置为我最初安装它们时已进行硬编码的值.我一直在跟踪用户的位置,并且随着电话位置的任何变化,地图都应通过CLLocationManagerDelegate和下面列出的委托方法进行更新.

It would seem when the MKMapView is refreshed it resets the region to the values that have been hardcoded when I initially installed them. I am keeping track of the user's location and with any changes in the location of the phone the map should update via the CLLocationManagerDelegate and the delegate method listed below.

当前,我在locationManager:didUpdateToLocation:fromLocation中拥有这个

Currently I have this in the locationManager:didUpdateToLocation:fromLocation:

    MKCoordinateSpan span;
    span.latitudeDelta =  0.8;  
    span.longitudeDelta = 0.8;     
    MKCoordinateRegion region;
    region.center = newLocation.coordinate;
    region.span = span;
    [self.MyMapView setRegion:region animated:NO];

我也曾尝试将类似的代码放入viewDidLoad:无济于事.我的想法是,我可以通过某种方式在

I have also tried putting similar code in the viewDidLoad: to no avail. My thinking is that I could somehow set the region dynamically in the

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

委托方法我可以完全避开此问题,但是我不完全不确定应该怎么做.

delegate method I could sidestep this issue entirely but I am not exactly sure how I should do that.

最终,我只想让地图停止捕捉"回上述范围.预先感谢.

Ultimately I'd just like to have the map stop "snapping" back to the above span. Thanks in advance.

推荐答案

如果要使地图保持用户当前位置为中心,请首先确定是否要使用:

If you're trying to keep the map centered on the user's current location, first decide whether you want to use:

  • 地图视图的userLocation属性和地图视图的委托方法mapView:didUpdateUserLocation:,或
  • CLLocationManager及其委托方法locationManager:didUpdateToLocation:fromLocation:
  • the map view's userLocation property and the map view's delegate method mapView:didUpdateUserLocation:, or
  • the CLLocationManager and its delegate method locationManager:didUpdateToLocation:fromLocation:

对于任何一个,在viewDidLoad中,您应该只设置地图视图的初始区域,包括中心(使用一些默认坐标)和跨度.用户的位置不太可能立即在viewDidLoad中可用,因为它可能需要花费几秒钟来获取.

With either one, in viewDidLoad, you should just set the map view's initial region including center (using some default coordinate) and span. The user's location will be unlikely to be available immediately in viewDidLoad as it can take a few seconds to acquire.

在设置userLocation之前尝试使用userLocation可能会导致在使用该值设置区域时引起异常.

Trying to use userLocation before its set may cause an exception when setting the region with that value.

如果使用地图视图的userLocation,则:

If using the map view's userLocation, then:

  • 在viewDidLoad或IB中,将mapView.showsUserLocation设置为YES
  • mapView:didUpdateUserLocation:方法中,执行mapView.centerCoordinate = userLocation.location.coordinate;(或使用setCenterCoordinate:animated:)
  • 建议还实施mapView:didFailToLocateUserWithError:来处理或至少意识到故障
  • in viewDidLoad or IB, set mapView.showsUserLocation to YES
  • in the mapView:didUpdateUserLocation: method, do mapView.centerCoordinate = userLocation.location.coordinate; (or use setCenterCoordinate:animated:)
  • recommend also implementing mapView:didFailToLocateUserWithError: to handle or at least become aware of failures

如果使用CLLocationManager,则:

  • 在viewDidLoad中,执行[locationManager startUpdatingLocation];
  • locationManager:didUpdateToLocation:fromLocation:方法中,执行mapView.centerCoordinate = newLocation.coordinate;(或使用动画方法)
  • 建议还实施locationManager:didFailWithError:来处理或至少意识到故障
  • in viewDidLoad, do [locationManager startUpdatingLocation];
  • in the locationManager:didUpdateToLocation:fromLocation: method, do mapView.centerCoordinate = newLocation.coordinate; (or use the animated method)
  • recommend also implementing locationManager:didFailWithError: to handle or at least become aware of failures

这篇关于我如何动态设置MKMapView的区域跨度,以使其不会“捕捉"到屏幕上.重绘地图后又回到初始区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆