开发者

How to add UIView to MKMap?

How to add UIView to MKMap so that when we move map around, UIView also moves with map, i mean to say that generally when we add UIView (or any UI component) to MKMap and then if we either zoom / move map a开发者_JAVA百科round, UIView remain fixed at its initial position, i need as map moves UIView also moves with it.


Assuming i'm understanding this correctly, you want to be able to add a custom view at a specific coordinate on an MKMapView.

If so, MKMapView terms them as Annotations, and the MKMapView class exposes an addAnnotation: and addAnnotations: method. These methods need to be passed an object that adheres to the MKAnnotation protocol, which means your object must provide a title, subtitle and coordinate (which is a CLLocationCoordinate2D).

Important to note; this object is NOT your UIView, it's simply an object that provides data to your view.

In order to actually add the new you need an object that implements the MKMapViewDelegate protocol, i'd suggest you use a UIViewController subclass for this (which includes your mapview, etc).

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

  // Instiate your UIView. The annotation passed into this method is the annotation your passed to the mapview's addAnnotation method, so you can cast this and access any application specific data.
}

And that's pretty much it. Add an annotation to your mapview, and the mapview asks it's delegate to provide a view for that annotation.


You may want to have a look at the MKAnnotationView Class Reference, especially the part about managing callout views.

If that level of control is not enough, take a look here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜