我正在尝试制作一个使用mapkit来显示自定义引脚的应用。其中一个要求是对引脚进行群集并显示群集引脚的数量。
问题在于我试图做到这一点很快,而且没有解决方案。我尝试了不同的库,我设法让它工作的唯一一个就是这个:
https://github.com/RVLVR/REVClusterMap
我能够添加注释,当我缩小时它们会聚集,但我无法更改它们的图片。这是我到目前为止所得到的:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myMap: REVClusterMapView!
let regionRadius: CLLocationDistance = 1000
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 43.719880, longitude: -79.467060)
centerMapOnLocation(initialLocation)
var myAnn: NSMutableArray = NSMutableArray()
let coordonate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 43.717880, longitude: -79.462060)
var ann = CustomPointAnnotation()
ann.coordinate = coordonate
ann.title = "MyPin"
ann.subtitle = "some text"
ann.imageName = "restaurant_pin"
myAnn.addObject(ann)
myMap.addAnnotations(myAnn as [AnyObject])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
myMap.setRegion(coordinateRegion, animated: true)
}
func myMap(myMap: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("delegate called")
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = myMap.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
//Set annotation-specific properties **AFTER**
//the view is dequeued or created...
let cpa = annotation as! CustomPointAnnotation
println(cpa.imageName)
anView.image = UIImage(named: cpa.imageName)
return anView
}
class CustomPointAnnotation: REVClusterPin {
var imageName: String!
}
}
在控制台中没有"委托称为"信息。如何让它调用它并且这个委托好吗?
在Objective-C中它看起来像这样:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation class] == MKUserLocation.class) {
//userLocation = annotation;
return nil;
}
REVClusterPin *pin = (REVClusterPin *)annotation;
MKAnnotationView *annView;
if( [pin nodeCount] > 0 ){
pin.title = @"___";
annView = (REVClusterAnnotationView*)
[mapView dequeueReusableAnnotationViewWithIdentifier:@"cluster"];
if( !annView )
annView = (REVClusterAnnotationView*)
[[[REVClusterAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"cluster"] autorelease];
annView.image = [UIImage imageNamed:@"cluster.png"];
[(REVClusterAnnotationView*)annView setClusterText:
[NSString stringWithFormat:@"%i",[pin nodeCount]]];
annView.canShowCallout = NO;
} else {
annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if( !annView )
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"pin"] autorelease];
annView.image = [UIImage imageNamed:@"pinpoint.png"];
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-6.0, 0.0);
}
return annView;
}
我很抱歉这个愚蠢的问题,但我已经挣扎了好几天找到一个工作的图书馆,现在我无法理解这一点。此外,我是IOS的新手,Objective-C对初学者来说是一门非常难的语言。
谢谢!