問題描述
(使用 iOS 5 和 Xcode 4.2)
我有一個 MKMapView,想在用戶位置周圍畫一個半徑為 1000m 的圓.
I have an MKMapView and want to draw a circle of 1000m radius around the user location.
從表面上看,實現 mapView:viewForAnnotation: 地圖視圖委托方法,并為用戶位置添加自定義 MKAnnotationView,將是一個完美的解決方案.它看起來像這樣:
On the surface it would seem that implementing the mapView:viewForAnnotation: map view delegate method, and adding a custom MKAnnotationView for the users location, would be a perfect solution. It would look something like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, return my custom MKAnnotationView.
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return myCustomAnnotationView;
} else {
return nil;
}
}
但是,當您放大和縮小地圖時,地圖上的注釋不會按比例縮放.
However annotations on the map don't scale when you zoom in and out of the map.
所以我嘗試使用 MKCircle 類并將其坐標設置為來自我的 locationManger/map 視圖委托的最新坐標.但是作為 坐標屬性MKCircle 是只讀的,我必須刪除覆蓋,然后在每次用戶移動時添加一個新的.發生時會引起明顯的閃爍.
So I tried adding an overlay (because overlays scale with the map), using the MKCircle class and setting its co-ordinates to the latest co-ordinates from my locationManger/map view delegate. However as the coordinate property of MKCircle is readonly, I'm having to remove the overlay then add a new one each time the user moves. Causing a noticeable flicker as it happens.
當地圖視圖被放大和縮小時,有什么方法可以使注釋無縫地縮放?或者有沒有一種好方法可以讓疊加層隨著用戶位置的變化無縫移動?
Is there any way to make an annotation scale seamlessly as the map view is scaled in and out? Or is there a good way to make an overlay move seamlessly with changes in the users location?
非常感謝您的幫助:)
推薦答案
嘗試自定義疊加層.在 viewDidLoad 中添加:
Try a custom overlay. Add this in viewDidLoad:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];
userLocation 可以通過將 MKUserLocationAnnotation 存儲為屬性來獲得.然后,要實際繪制圓圈,請將其放入地圖視圖的委托中:
userLocation can be obtained by storing the MKUserLocationAnnotation as a property. Then, to actually draw the circle, put this in the map view's delegate:
- (MKOverlayRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
return circleView;
}
這篇關于在 MKMapView 中圍繞用戶位置畫一個半徑為 1000m 的圓的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!