MKMapView自定义UserLocation注释

时间:2011-03-09 18:31:40

标签: iphone ios ipad mkmapview

我希望自定义MKMapView userLocation注释,以绘制像官方地图应用程序一样的标题箭头。

我正在尝试在viewForAnnotation中执行此操作:

 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

    }
 }

但是我无法找到接下来要做的事情来获取UserLocation的视图并自定义它。

非常感谢......

3 个答案:

答案 0 :(得分:6)

我创建了SVPulsingAnnotationView,这是MKUserLocationView的可自定义副本。

enter image description here

您实例化它就像任何其他MKAnnotationView

一样
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if([annotation isKindOfClass:[MyAnnotationClass class]]) {
        static NSString *identifier = @"currentLocation";
        SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            pulsingView.annotationColor = [UIColor colorWithRed:0.678431 green:0 blue:0 alpha:1];
        }

        pulsingView.canShowCallout = YES;
        return pulsingView;
    }

    return nil;
}

答案 1 :(得分:5)

默认用户位置标记的类(至少在SDK 4.2中)是MKUserLocationView,但这不是公共的,因此您无法创建要修改的实例而不会有来自应用商店的拒绝风险。您必须改为创建自己的MKAnnotationView(或MKAnnotationView的子类)。

答案 2 :(得分:0)

导入UIKit 导入MapKit 导入CoreLocation

class secondViewController:UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

@IBOutlet var mapview: MKMapView!

// var locationmanager = CLLocationManager()

let locationManager = CLLocationManager()
var currentLocation: CLLocation!

/ * public func locationManager(_ manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){

    let location = locations[0]

    let span = MKCoordinateSpanMake(0.1, 0.1)

   let my = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

    print(my)
    let region = MKCoordinateRegionMake(my, span)

    mapview.setRegion(region, animated: true)

    self.mapview.showsUserLocation = true
}*/

override func viewDidLoad() {
    super.viewDidLoad()

 /*   mapview.mapType = MKMapType.standard
    locationmanager.delegate = self
    locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    locationmanager.requestWhenInUseAuthorization()
 /*   isAuthorizedtoGetUserLocation()
    if CLLocationManager.locationServicesEnabled() {
        locationmanager.delegate = self
        locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    }*/

    locationmanager.startUpdatingLocation()*/