MKMapView : Annotation Pin 대신 사용자 지정보기
MKMapView
작은 바위 핀 대신 내 이미지를 표시하고 싶습니다 . 누군가 여기에 유용한 코드를 입력하거나 방법을 알려주시겠습니까?
감사!
편집하다
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
내 이미지 pinks.jpg 가 기본 핀보기 ( 바위 핀 모양 ) 대신 위치를 고정하여지도에있을 것으로 예상하고 있습니다 . 하지만 여전히 핀의 기본 이미지를 얻고 있습니다.
주석보기에 고유 한 이미지를 사용 MKAnnotationView
하려면 MKPinAnnotationView
.
MKPinAnnotationView
의 하위 클래스 MKAnnotationView
이므로 image
속성이 있지만 일반적으로이를 재정의하고 핀 이미지를 그립니다 (그게 목적입니다).
따라서 코드를 다음과 같이 변경하십시오.
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
//pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
공지 사항 animatesDrop
해당 속성 만 존재하기 때문에 또한 주석 MKPinAnnotationView
.
그래도 이미지 주석을 삭제하려면 애니메이션을 직접 수행해야합니다. Stack Overflow에서 "animatesdrop mkannotationview"를 검색하면 몇 가지 답변을 찾을 수 있습니다. 다음은 처음 두 가지입니다.
- MKPinAnnotationView가 아닌 MKAnnotationView에서 animatesDrop을 호출 할 수 있습니까?
- MKAnnotationView를 사용하여 어떻게 사용자 지정 "핀 드롭"애니메이션을 만들 수 있습니까?
Swift 3 에 대한 답변이 있습니다 . 가능한 경우 주석보기를 대기열에서 빼거나 그렇지 않은 경우 새보기를 만듭니다.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
Swift 2.2 :
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
return nil
}
// Better to make this class property
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: "yourImage")
}
return annotationView
}
I agree with with answer of the Anna and i like to show how will look that in swift3.This answer it's with many other options.Like a resize the image, get a list of images from array and ect.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? PetrolStation {
let identifier = "pinAnnotation"
var view: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView { // 2
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 3
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
//here We put a coordinates where we like to show bubble with text information up on the pin image
view.calloutOffset = CGPoint(x: -7, y: 7)
//Here this is a array of images
let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace]
//Here we set the resize of the image
let size = CGSize(width: 30, height: 30)
UIGraphicsBeginImageContext(size)
pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
view.image = resizeImage
//Here we like to put into bubble window a singe for detail Informations
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
//Here we make change of standard pin image with our image
view.image = resizeImage
}
return view
}
return nil
}
참고URL : https://stackoverflow.com/questions/9814988/mkmapview-instead-of-annotation-pin-a-custom-view
'developer tip' 카테고리의 다른 글
Picturebox에서 스크롤바를 얻는 방법 (0) | 2020.11.30 |
---|---|
파이썬 부동 소수점 값을 가능한 최소로 증가시킵니다. (0) | 2020.11.30 |
직접 URL 액세스에서 파일을 방지하는 방법은 무엇입니까? (0) | 2020.11.30 |
HTML, 자바 스크립트 및 Ajax 요청으로 jQuery를 사용하여 Amazon s3에 이미지 업로드 (PHP 없음) (0) | 2020.11.30 |
BigDecimal을 문자열로 (0) | 2020.11.30 |