在我的应用中,我有一个mapView和其他一些组件
我已将注释视图更改为我自己的图标,图标显示在纬度和经度的特定位置。
但是,当我点击并按住图标时,它会自动转换为anotations
请帮帮我
加载时这是我的地图
当我点击并按住注释
时,它就变成了这样
didUpdateUserLocation
{
CLLocationCoordinate2D first;
first.latitude=13.040202;
first.longitude=80.24298;
myAnnotation.coordinate=first;
[locations addObject:myAnnotation];
[self.mapView addAnnotations:locations];
}
viewForAnnotation:
{
static NSString *identifier = @"Wifintech";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if ( pinView == nil )
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
pinView.image = [UIImage imageNamed:@"car-side.png"];
pinView.canShowCallout = YES;
}
didSelectAnnotationView
{
float latitude = [[view annotation ] coordinate].latitude;
float longitude = [[view annotation ] coordinate].longitude;
title_value=[[view annotation] title];
NSString * subtitle_val =[[view annotation] subtitle];
title_para.text=[NSString stringWithFormat:@"%@, %@",title_value,subtitle_val];
latitude_value.text=[NSString stringWithFormat:@"%f",latitude];
longitude_value.text=[NSString stringWithFormat:@"%f",longitude];
}
答案 0 :(得分:1)
尝试从
返回MKAnnotationView而不是MKPinAnnotationView-mapView:viewForAnnotation:
子类很可能会覆盖某些跟踪视图选定状态和调整图像的方法。我怀疑使用MKAnnotationView时不会出现这种情况。
答案 1 :(得分:1)
使用自己的图像时,需要创建MKAnnotationView而不是MKPinAnnotationView。在发布问题之前,请尝试搜索SO以获得答案(这将为您节省大量时间)。
答案 2 :(得分:0)
我认为您需要仔细查看以下文档。 另外,也许确保didSelect ..方法中的视图实际上是一个pinAnnotionView,并且图像就是你想象的那样。
从MKAnnotationView上的文档:
注释视图支持选择状态的概念,该选择状态确定是否取消选择,选择或选择视图并显示标准标注视图。用户通过与注释视图的交互在选择状态之间切换。在未选定状态下,将显示注释视图但不突出显示。在选定状态下,注释会突出显示,但不会显示标注。最后,可以使用突出显示和标注来显示注释。标注视图显示其他信息,例如标题字符串和用于查看更多信息的控件。标题信息由注释对象提供,但注释视图负责提供任何自定义控件。有关更多信息,请参阅子类注释。
答案 3 :(得分:0)
这是我的代码
#import "FirstViewController.h"
#import "LocationObject.h"
#define THE_SPAN 0.01f;
@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *latitude_value;
@property (weak, nonatomic) IBOutlet UITextField *longitude_value;
@property (copy, nonatomic) NSString *title_value;
@property(nonatomic,assign) CLLocationCoordinate2D myCoordinate;
@property(nonatomic,copy) NSMutableArray *locations;
@property(nonatomic,retain) MKPointAnnotation * myAnnotation;
@property (readwrite) int tag;
@property(retain) MKPinAnnotationView *pinView;
@property (weak, nonatomic) IBOutlet UITextView *title_para;
@property(assign) CLLocationCoordinate2D *coordsArray;
@property(nonatomic,assign) MKPolyline * routeLine;
@end
@implementation FirstViewController
@synthesize latitude_value;
@synthesize longitude_value;
@synthesize title_value;
@synthesize myCoordinate;
@synthesize mapView;
@synthesize locations;
@synthesize myAnnotation;
@synthesize tag;
@synthesize pinView;
@synthesize title_para;
@synthesize title;
@synthesize coordsArray;
@synthesize routeLine;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setMapView:nil];
[self setLatitude_value:nil];
[self setLongitude_value:nil];
[self setLatitude_value:nil];
[self setLongitude_value:nil];
[self setTitle_value:nil];
[self setTitle_para:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polyLineView = [[MKPolylineView alloc] initWithPolyline:routeLine];
polyLineView.fillColor = [UIColor blueColor];
polyLineView.strokeColor = [UIColor redColor];
polyLineView.lineWidth = 2;
return polyLineView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//地图视图:区域nad注释
#pragma mark map view delegate methods
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//map region and center location
MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
center.latitude=13.040223;
center.longitude=80.240995;
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;
myRegion=MKCoordinateRegionMake(center, span);
MKCoordinateRegion adjusted_region=[self.mapView regionThatFits:myRegion];
[self.mapView setRegion:adjusted_region animated:YES];
locations=[[NSMutableArray alloc]init];
//first point
CLLocationCoordinate2D first;
first.latitude=13.040202;
first.longitude=80.24298;
myAnnotation = [[MKPointAnnotation alloc]init];
myAnnotation.coordinate=first;
myAnnotation.title=@"Wifin Technology";
myAnnotation.subtitle=@"Globus";
[locations addObject:myAnnotation];
//second point
CLLocationCoordinate2D second;
second.latitude=13.0406527;
second.longitude=80.2437427;
myAnnotation= [[MKPointAnnotation alloc]init];
myAnnotation.coordinate=second;
myAnnotation.title=@"The Residency Towers";
myAnnotation.subtitle=@"Chennai";
[locations addObject:myAnnotation];
//third point
CLLocationCoordinate2D third;
third.latitude=13.040202;
third.longitude=80.240191;
myAnnotation= [[MKPointAnnotation alloc]init];
myAnnotation.coordinate=third;
myAnnotation.title=@"Rado Cool Zone";
myAnnotation.subtitle=@"Chennai";
[locations addObject:myAnnotation];
[self.mapView addAnnotations:locations];
title_para.text=[[NSString alloc]initWithString:@"The position displayed is Pondy Bazaar"];
coordsArray = malloc(sizeof(CLLocationCoordinate2D) * locations.count);
int i = 0;
for (CLLocation *loc in locations) {
coordsArray[i] = loc.coordinate;
i++;
}
routeLine = [MKPolyline polylineWithCoordinates:coordsArray
count:locations.count];
[mapView addOverlay:routeLine];
self.longitude_value.text=[NSString stringWithFormat:@"%f",center.longitude];
self.latitude_value.text=[NSString stringWithFormat:@"%f",center.latitude];
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
float latitude = [[view annotation ] coordinate].latitude;
float longitude = [[view annotation ] coordinate].longitude;
title_value=[[view annotation] title];
NSString * subtitle_val =[[view annotation] subtitle];
title_para.text=[NSString stringWithFormat:@"%@, %@",title_value,subtitle_val];
latitude_value.text=[NSString stringWithFormat:@"%f",latitude];
longitude_value.text=[NSString stringWithFormat:@"%f",longitude];
}
//注释视图:图标
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
pinView = nil;
static NSString *identifier = @"Wifintech";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if ( pinView == nil )
pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:identifier];
if([[annotation title] isEqualToString:@"Wifin Technology"])
{
pinView.image = [UIImage imageNamed:@"car-side.png"];
pinView.canShowCallout = YES;
}
else if ([[annotation title] isEqualToString:@"The Residency Towers"])
{
pinView.image=[UIImage imageNamed:@"lorry-side.png"];
pinView.canShowCallout = YES;
}
else if([[annotation title] isEqualToString:@"Rado Cool Zone"])
{
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
//pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:@"car.png"];
}
else {
NSLog(@"Nothing");
}
return pinView;
}
@end