ユーザーの位置を示す青い点を画像に変更することはできMKMapView
ますか? たとえば、小さな車やその他の.png
画像はありますか?
37384 次
5 に答える
56
MKMapViewDelegateのviewForAnnotation :メソッドでは、おそらく次のようなコードになります。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
注釈がuserLocationの場合はnilを返し、 mapViewに青い点と円のアニメーションを表示させます。userLocationのカスタム アノテーションを表示するには、その行を削除してカスタマイズを行います。return nil;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation){
customPinView.image = [UIImage imageNamed:@"myCarImage.png"];
}
else{
customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
}
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
于 2011-06-09T13:47:17.777 に答える
4
これは、複数のピンを持つ可能性のある Swift 2.0 バージョンです。
このコードでは、CustomAnnotation は単なる MKAnnotation サブクラスです。基本的に、注釈がカスタム クラスの種類でない場合は、ユーザーの場所のピンです。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
// This is false if its a user pin
if(annotation.isKindOfClass(CustomAnnotation) == false)
{
let userPin = "userLocation"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin)
{
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin)
mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME)
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
return mkAnnotationView
}
}
let annotation = annotation as? CustomAnnotation
if(annotation == nil)
{
return nil
}
let endPointsIdentifier = "endPoint"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier)
{
dequeuedView.image = annotation!.uiimage
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier)
mkAnnotationView.image = annotation!.uiimage
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:")
mkAnnotationView.addGestureRecognizer(gesture)
return mkAnnotationView
}
}
于 2015-09-22T23:33:05.547 に答える
0
このようなものを試してみてください。Xcode 7とswift 2で私のために働いています。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
annotationView.image = UIImage(named: "icon_coordinates_self")
return annotationView
//return nil
}
// for other annotation except current location
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "Annotation_map")
}
return annotationView
}
于 2016-09-16T09:57:43.740 に答える