iOS高德地图 多个大头针显示图片 点击效果
2016-11-17 17:40
686 查看
最近做一款有关地图的软件 使用到了高德地图 将一些经验记录下来 第一篇博客 以后会陆续更新 希望看到的朋友多多支持啦。
导入等过程PASS掉。
新建一个类 继承 MAPointAnnotation 添加图片属性
@property (nonatomic,copy)NSString *imageString;
在viewController.m 中添加模拟数据
self.annotations = [NSMutableArrayarray];
CLLocationCoordinate2D coordinates[10] = {
{31.34201706,104.19923639},
{31.34131706,104.19943639},
{31.34241706,104.19973639},
{31.34111706,104.20093639}};
for (int i =0; i <4; ++i)
{
ZSPointAnnotation *a1 = [[ZSPointAnnotationalloc]init];
a1.coordinate = coordinates[i];
a1.title = [NSStringstringWithFormat:@"标题: %d", i];
a1.imageString = [NSStringstringWithFormat:@"图%d.jpg",i];
[self.annotationsaddObject:a1];
}
模拟4条数据
创建地图添加annotations 遵循代理MAMapViewDelegate
self.mapView.delegate =self;
[self.mapViewaddAnnotations:self.annotations];
[self.mapViewshowAnnotations:self.annotationsedgePadding:UIEdgeInsetsMake(20,20,20,80)animated:YES];
实现代理方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
customAnnotationView参考高德demo中 自定义样式标注
staticNSString *customReuseIndetifier =@"customReuseIndetifier";
CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView ==nil)
{
annotationView = [[CustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:customReuseIndetifier];
annotationView.canShowCallout =NO;
annotationView.draggable =YES;
annotationView.calloutOffset =CGPointMake(0, -5);
}
这里我们遍历数组 拿到point 与地图上annotation上title比较 相同的话 设置图片为模拟数据的image
for (ZSPointAnnotation *pointinself.annotations) {
if ([[annotationtitle]isEqualToString:point.title]) {
annotationView.portraitImageView.image = [UIImageimageNamed:point.imageString];
annotationView.portraitImageView.layer.cornerRadius =25;
annotationView.portraitImageView.layer.masksToBounds =YES;
}
}
return annotationView;
}
接下来实现它的点击方法 方法相类似
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
NSArray * array = [NSArrayarrayWithArray:_mapView.annotations];
这里遍历mapView上的annotations 比较经度(纬度)title 都可以 这样就可以啦
for (ZSPointAnnotation *pointin array) {
if (view.annotation.coordinate.latitude == point.coordinate.latitude){
NSLog(@"点了我 %@",point.title);
NSLog(@"图片是我 %@",point.imageString);
}else{
NSLog(@"没点我 %@",point.title);
}
}
}
导入等过程PASS掉。
新建一个类 继承 MAPointAnnotation 添加图片属性
@property (nonatomic,copy)NSString *imageString;
在viewController.m 中添加模拟数据
self.annotations = [NSMutableArrayarray];
CLLocationCoordinate2D coordinates[10] = {
{31.34201706,104.19923639},
{31.34131706,104.19943639},
{31.34241706,104.19973639},
{31.34111706,104.20093639}};
for (int i =0; i <4; ++i)
{
ZSPointAnnotation *a1 = [[ZSPointAnnotationalloc]init];
a1.coordinate = coordinates[i];
a1.title = [NSStringstringWithFormat:@"标题: %d", i];
a1.imageString = [NSStringstringWithFormat:@"图%d.jpg",i];
[self.annotationsaddObject:a1];
}
模拟4条数据
创建地图添加annotations 遵循代理MAMapViewDelegate
self.mapView.delegate =self;
[self.mapViewaddAnnotations:self.annotations];
[self.mapViewshowAnnotations:self.annotationsedgePadding:UIEdgeInsetsMake(20,20,20,80)animated:YES];
实现代理方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
customAnnotationView参考高德demo中 自定义样式标注
staticNSString *customReuseIndetifier =@"customReuseIndetifier";
CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView ==nil)
{
annotationView = [[CustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:customReuseIndetifier];
annotationView.canShowCallout =NO;
annotationView.draggable =YES;
annotationView.calloutOffset =CGPointMake(0, -5);
}
这里我们遍历数组 拿到point 与地图上annotation上title比较 相同的话 设置图片为模拟数据的image
for (ZSPointAnnotation *pointinself.annotations) {
if ([[annotationtitle]isEqualToString:point.title]) {
annotationView.portraitImageView.image = [UIImageimageNamed:point.imageString];
annotationView.portraitImageView.layer.cornerRadius =25;
annotationView.portraitImageView.layer.masksToBounds =YES;
}
}
return annotationView;
}
接下来实现它的点击方法 方法相类似
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
NSArray * array = [NSArrayarrayWithArray:_mapView.annotations];
这里遍历mapView上的annotations 比较经度(纬度)title 都可以 这样就可以啦
for (ZSPointAnnotation *pointin array) {
if (view.annotation.coordinate.latitude == point.coordinate.latitude){
NSLog(@"点了我 %@",point.title);
NSLog(@"图片是我 %@",point.imageString);
}else{
NSLog(@"没点我 %@",point.title);
}
}
}
相关文章推荐
- android高仿微信UI点击头像显示大图片效果
- iOS 点击cell改变背景颜色或者图片&&cell中其他部分取消高亮显示
- iOS开发之 WebView点击图片看大图效果
- android高仿微信UI点击头像显示大图片效果
- android仿IOS按钮点击效果,一张图片实现
- GridView使用lightbox,鼠标点击显示图片效果
- IOS自定义图片浏览器,支持浏览多张图片,点击,双击,两手指缩放操作,并显示加载进度条
- android高仿微信UI点击头像显示大图片效果
- android高仿微信UI点击头像显示大图片效果
- [置顶] ios 网页中图片点击放大效果demo
- android高仿微信UI点击头像显示大图片效果, Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
- jquery特效---点击图片显示大图片效果
- IOS-UIBarButtonItem的图片文字点击效果
- GoogleMap_IOS MKMapView如何实现不点击大头针,就可以出现calloutView效果
- [置顶] ios 网页中图片点击放大效果demo
- ios 地图大头针自定义显示图片 MKAnnotationView
- IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果
- ios 点击按钮,显示遮盖层的效果
- android高仿微信UI点击头像显示大图片效果