問(wèn)題描述
(使用 iOS 5 和 Xcode 4.2)
我有一個(gè) MKMapView,想在用戶位置周圍畫一個(gè)半徑為 1000m 的圓.
I have an MKMapView and want to draw a circle of 1000m radius around the user location.
從表面上看,實(shí)現(xiàn) mapView:viewForAnnotation: 地圖視圖委托方法,并為用戶位置添加自定義 MKAnnotationView,將是一個(gè)完美的解決方案.它看起來(lái)像這樣:
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;
}
}
但是,當(dāng)您放大和縮小地圖時(shí),地圖上的注釋不會(huì)按比例縮放.
However annotations on the map don't scale when you zoom in and out of the map.
所以我嘗試使用 MKCircle 類并將其坐標(biāo)設(shè)置為來(lái)自我的 locationManger/map 視圖委托的最新坐標(biāo).但是作為 坐標(biāo)屬性MKCircle 是只讀的,我必須刪除覆蓋,然后在每次用戶移動(dòng)時(shí)添加一個(gè)新的.發(fā)生時(shí)會(huì)引起明顯的閃爍.
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.
當(dāng)?shù)貓D視圖被放大和縮小時(shí),有什么方法可以使注釋無(wú)縫地縮放?或者有沒(méi)有一種好方法可以讓疊加層隨著用戶位置的變化無(wú)縫移動(dòng)?
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 可以通過(guò)將 MKUserLocationAnnotation 存儲(chǔ)為屬性來(lái)獲得.然后,要實(shí)際繪制圓圈,請(qǐng)將其放入地圖視圖的委托中:
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;
}
這篇關(guān)于在 MKMapView 中圍繞用戶位置畫一個(gè)半徑為 1000m 的圓的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!