(使用iOS 5和Xcode 4.2)
我有一个MKMapView,想画一个半径1000米的圆围绕用户的位置。
从表面上看,实现mapView:viewForAnnotation:地图视图委托方法并为用户位置添加自定义MKAnnotationView似乎是一个完美的解决方案。看起来会是这样的:
- (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;
}
}但是,地图上的注释在缩放地图时不会缩放。
因此,我尝试添加一个覆盖(因为覆盖比例尺与地图),使用MKCircle类并将其协调设置为来自我的locationManger/map视图委托的最新协调。然而,由于坐标性质 of MKCircle是只读的,我不得不删除覆盖,然后每次用户移动时添加一个新的覆盖。在发生时引起明显的闪烁。
在地图视图被缩放时,是否有任何方法可以无缝地进行注释缩放?或者有一个很好的方法,使覆盖移动与用户位置的变化无缝?
我非常感谢你的帮助:)
发布于 2012-02-10 11:55:46
尝试一个定制的覆盖层。在viewDidLoad中添加以下内容:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];userLocation可以通过将MKUserLocationAnnotation存储为属性来获得。然后,要实际绘制圆圈,将其放在map视图的委托中:
- (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;
}发布于 2014-09-25 12:58:07
使用Swift更新的iOS 8.0版本。
import Foundation
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var locationManager: CLLocationManager = CLLocationManager()
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// We use a predefined location
var location = CLLocation(latitude: 46.7667 as CLLocationDegrees, longitude: 23.58 as CLLocationDegrees)
addRadiusCircle(location)
}
func addRadiusCircle(location: CLLocation){
self.mapView.delegate = self
var circle = MKCircle(centerCoordinate: location.coordinate, radius: 10000 as CLLocationDistance)
self.mapView.addOverlay(circle)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKCircle {
var circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.redColor()
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
return circle
} else {
return nil
}
}
}发布于 2016-12-14 02:35:55
Swift 3/ Xcode 8在这里:
func addRadiusCircle(location: CLLocation){
if let poll = self.selectedPoll {
self.mapView.delegate = self
let circle = MKCircle(center: location.coordinate, radius: 10)
self.mapView.add(circle)
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.red
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
return circle
} else {
return MKPolylineRenderer()
}
}然后像这样打电话:
self.addRadiusCircle(location: CLLocation(latitude: YOUR_LAT_HERE, longitude: YOUR_LNG_HERE))https://stackoverflow.com/questions/9056451
复制相似问题