在 iPhone 地图上移动 MKPinAnnotation 时出现问题
我正在构建一个在地图上跟踪用户位置的应用程序。我可以轻松插入图钉,但是当位置更改时,应用程序会退出。
我已在 viewdidload: 方法中将默认坐标设置为 0,0,并在该位置添加了一个引脚。我这样做是因为我想在位置更新时删除图钉,然后在新位置再次插入图钉。
这是我在 -(void)locationManager: didUpdateToLocation:fromLocation: 方法中编写的代码。
[mapview removeAnnotation:myannotation];
CLLocationCoordinate2D currentlocation;
currentlocation.latitude=newLocation.coordinate.latitude;
currentlocation.longitude=newLocation.coordinate.longitude;
myannotation=[[[CSMapAnnotation alloc]initWithCoordinate:currentlocation annotationType:CSMapAnnotationTypeStart title:@"My Location"] autorelease];
[mapview addAnnotation:myannotation];
这里,myannotation是我要添加的pin,newLocation是更新的位置,CSMapAnnotation将返回注释视图。
问题是我的应用程序每次都会崩溃。在崩溃之前我能够在控制台上收到警告消息。以下是消息:
“CSMapAnnotation 类的实例 0x182020 正在被释放,而键值观察者仍在其中注册。观察信息正在泄漏,甚至可能错误地附加到其他对象。”
所以,如果有人遇到同样的问题,请帮忙...
I am building an app that tracks user location on map. I can insert a pin easily, but when the location is changed, the app quits.
I have set the default coordinates to 0,0 in viewdidload: method and I have added a pin at that location. I have done this because I want to remove the pin when location is updated and then insert the pin again on new location.
Here is the code which I have written in -(void)locationManager: didUpdateToLocation:fromLocation: method.
[mapview removeAnnotation:myannotation];
CLLocationCoordinate2D currentlocation;
currentlocation.latitude=newLocation.coordinate.latitude;
currentlocation.longitude=newLocation.coordinate.longitude;
myannotation=[[[CSMapAnnotation alloc]initWithCoordinate:currentlocation annotationType:CSMapAnnotationTypeStart title:@"My Location"] autorelease];
[mapview addAnnotation:myannotation];
Here, myannotation is the pin which I want to add,newLocation is the updated location and CSMapAnnotation will return annotation view.
The problem is my app crashes everytime. I was able to get warning message on console before crashing. Here is the message:
"An instance 0x182020 of class CSMapAnnotation is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object."
So, plz help if anyone has faced the same problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要注意的一些事情:
MKMapView 已经有一个
showsUserLocation
属性,可以通过脉冲蓝点来跟踪用户的位置。如果您想在该位置单独设置固定,您可以从地图视图中获取userLocation
属性。崩溃错误可能是由于
autorelease
调用造成的。修复此问题的最简单方法是使myannotation
成为具有retain
属性的属性,然后取出自动释放,并使用而不是
.myannotation
self.myannotation不太清楚将“默认坐标设置为 0, 0”的含义。如果这些是纬度/经度,那么您将在英国格林威治放置一个图钉:-) 您实际上不需要不断添加和删除注释。您可以添加它们一次,然后根据需要调整它们的位置。不太
A few things to look out for:
MKMapView already has a
showsUserLocation
attribute that tracks the user's location for you with the pulsing blue-dot thing. If you want to separately set a pin on that location, you can get theuserLocation
attribute from the mapview.The crashing bug could be because of the
autorelease
call. Easiest way to fix it is to makemyannotation
a property with aretain
attribute, then take out the autorelease and instead ofmyannotation
useself.myannotation
.Not really clear what you mean by setting "default coordinates to 0, 0." If these are lat/longs then you're putting a pin in Greenwich, England :-) You don't really need to continually add and remove annotations. You can add them once then adjust their position as needed.
我认为它不喜欢您的 myannotation 自动释放标记,当您将注释添加到地图时,它可能不会被保留,然后它只是释放并崩溃,这就是我通过查看您发布的代码可以想到的。希望能帮助到你
I am thinking it is not liking your autorelease tag for myannotation, when you add the annotation to the map it might not be being retained, and then it just deallocates and crash, thats what i can think of from looking at the code you posted. Hope it helps
我刚刚遇到了同样的问题,它与注释的坐标有关。我已经忽略了 0,0 但由于某种原因用户输入 -180,-180 (可能来自我们无法确定位置的设备?)
无论如何 - 一旦我开始忽略 -180,-180 问题就消失了。
希望这可以帮助。
I just experienced this same problem and it had something to do with the coordinates of the annotation. I was already ignoring 0,0 but for some reason users were inputting -180,-180 (maybe from a device where we couldnt determine the location?)
anyways - as soon as I started ignoring -180,-180 the problem went away.
hope this helps.