Mk Mapview是否不断重置用户位置? [英] Mk Mapview keeps resetting on users location?

查看:113
本文介绍了Mk Mapview是否不断重置用户位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一张显示10个左右坐标的地图,该地图会在打开后立即获取用户的位置并以其为中心.当平移页面或缩放不同级别时,它最终会重置并居中于用户的第一个位置.我尝试过"stopupdating location"(动画位置),并且将Animated设置为"NO".当用户滚动页面时,我无法使其停留在正位置地图.

I currently have a map displaying 10 or so co ordinates.The map gets the users location and centers on it as soon as it is opened. When panning the page or zooming different levels it eventually resets and centers in on the first position of the user.I have tried "stopupdating location" and Animated as "NO".I can not get it to stay in positon when the user scrolls the map.

- (void)viewDidLoad {
[super viewDidLoad];
self.petrolMap.delegate = self;
self.location = [[CLLocationManager alloc] init];
[location setDelegate:self];
[location setDistanceFilter:0];  // Do not apply a distance filter to the map
[location setDesiredAccuracy:kCLLocationAccuracyBest]; // Use the best accuracy possible when displaying the map
petrolMap.delegate=self; // Display on "Petrol Map" , the mapview for the application}



-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{MKCoordinateRegion mapRegion;
mapRegion.center = petrolMap.userLocation.coordinate;
mapRegion.span.latitudeDelta=0.02;
mapRegion.span.longitudeDelta=0.02;
[petrolMap setRegion:mapRegion animated:NO];}

推荐答案

您的位置"是位置管理器,当计算出您所在的位置时,它将发送其委托人

Your 'location' is a location manager, when it works out where you are it'll send its delegate

locationManager:didUpdateToLocation:fromLocation:

您似乎没有的

,因此您在位置"上所做的所有设置都被浪费了(就您提供给我们的代码而言,它可能在其他地方很有用),并告诉它停止跟踪用户毫无用处.

which you don't seem to have, so all those settings you're doing to 'location' are wasted (as far as the code you've given us, it may be useful elsewhere) and telling it to stop tracking the user is of no use.

(无效)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {MKCoordinateRegion mapRegion;"是petrolMap发送给其委托的内容.您必须在某处设置了petrolMap来跟踪用户,可以在.xib中完成.

"(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{MKCoordinateRegion mapRegion;" is what petrolMap is sending to its delegate. Somewhere you must have set petrolMap to track the user, it can be done in the .xib.

无论如何,要停止petrolMap发送消息,请确保您运行

Anyway, to stop petrolMap sending messages make sure you run

[petrolMap setUserTrackingMode:MKUserTrackingModeNone animated:NO];

一些附加说明:

  • 在didUpdateUserLocation中,您不需要直接引用petrolMap,因为mapView参数设置为MKMapView发送消息的对象.

  • Within didUpdateUserLocation you don't need to refer to petrolMap directly because the mapView parameter is set to which ever MKMapView sent the message.

还在didUpdateUserLocation中,您使用的是petrolMap的userLocation而不是参数userLocation,甚至还在构建您的区域.该功能的整个代码可能只有一行

Also within didUpdateUserLocation you are using petrolMap's userLocation instead of the parameter userLocation, and even building your region. The entire code for that function could be one line

[mapView setRegion:mapRegion animated:NO];

  • 动画"控制区域更改的完成方式.是,表示它将在位置之间滑动,否,则意味着无论哪种方向,地图都将移动到新区域.

  • 'Animated' controls how the change in region is done. Yes means it will slide between locations, No means it will snap from one to the other instantly, either way the map will move to the new region.

    您的viewDidLoad方法可以切成两行,如下所示

    Your viewDidLoad method could be cut to two lines like follows

    [super viewDidLoad];
    self.petrolMap.delegate = self;
    

  • 附录:

    locationManager:didUpdateToLocation:fromLocation 
    

    在iOS6中已弃用.

    这篇关于Mk Mapview是否不断重置用户位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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