MapView可以与标注多个批注,如何通过标题下一个视图 [英] MapView with multiple annotations with callouts, how to pass title to next view

查看:192
本文介绍了MapView可以与标注多个批注,如何通过标题下一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形页面,用户preSS砸脚。可能有多个引脚在同一时间,每个标注视图都有推到当它是pressed堆栈一个新的视图标注。我想要做的就是通过注解视图的标题标签中的第二个观点。

下面是code,其中我把针:

   - (无效)preSS:(UILong pressGestureRecognizer *)识别
{
    CGPoint接触点= [识别locationInView:世界观];
    CLLocationCoordinate2D touchMapCoordinate = [世界观convertPoint:接触点toCoordinateFromView:世界观];    地理codeR = [[CLGeo codeR页头]初始化];
    CLLocation *位置= [[CLLocation页头] initWithCoordinate:touchMapCoordinate
                                                    海拔:CLLocationDistanceMax
                                          horizo​​ntalAccuracy:kCLLocationAccuracyBest
                                            verticalAccuracy:kCLLocationAccuracyBest
                                                   时间戳:[NSDate的日期]];
    [地理codeR reverseGeo codeLocation:位置
               completionHandler:^(* NSArray的地标,NSError *错误){
                   的NSLog(@reverseGeo codeR:completionHandler:被称为);
                   如果(错误){
                       的NSLog(@地理codeR失败,出现错误:%@,错误);
                   }其他{
                       CLPlacemark *地点= [标objectAtIndex:0];
                       地址= [的NSString stringWithFormat:@%@%@%@%@,[地方subThoroughfare],[地方通途],[地方局部性],[地方administrativeArea]];                       如果(UIGestureRecognizerStateBegan == [识别状态]){
                           addressPin = [[MapPoint的页头] initWithCoordinate:touchMapCoordinate
                                                                        标题:地址]。
                           [世界观addAnnotation:addressPin];
                       }
                   }
               }];
}

这里是code,其中我所说的第二种观点:

   - (空)的MapView:(*的MKMapView)的MapView annotationView:(MKAnnotationView *)查看calloutAccessoryControlTapped:(UIControl *)控制
{
   PinViewController * pinViewController = [[PinViewController的alloc]初始化];
    [个体经营passValues​​]
    [自我navigationController] pushViewController:pinViewController动画:是];
}


解决方案

您可以覆盖MKAnnotation(例如MyLocation)和MyLocation.h文件中声明

 #进口<基金会/ Foundation.h>
#进口< MapKit / MapKit.h>@interface MyLocation:NSObject的< MKAnnotation> {
    *的NSNumber identyfier;
    * NSString的_name;
    * NSString的_address;
    CLLocationCoordinate2D _coordinate;
}@属性(复制)的NSString *名称;
@属性(复制)的NSString *地址;
@属性(复制)的NSNumber * identyfier;
@属性(非原子,只读)CLLocationCoordinate2D协调; - (ID)initWithName:(* NSString的)名称地址:(* NSString的)地址
    坐标:(CLLocationCoordinate2D)坐标identyfier:(NSNumber的*)identyfier;

@end

在MyLocation.m文件:

 #进口MyLocation.h@implementation MyLocation@synthesize名= _name;
@synthesize地址= _address;
@synthesize坐标= _coordinate;
@synthesize identyfier = _identyfier; - (ID)initWithName:(* NSString的)名称地址:(* NSString的)地址
    坐标:(CLLocationCoordinate2D)坐标identyfier:(NSNumber的*)identyfier {
    如果((个体经营= [超级的init])){
        _name = [名副本];
        _address = [复制地址]
        _coordinate =协调;
        _identyfier = identyfier;
    }
    返回自我;
}

在你的地图视图在声明注释使用此methos:

  MyLocation *引脚= [[MyLocation页头] initWithName:place.name地址:place.address坐标:coordinate2D identyfier:SOME_ID];

因此​​,例如在地图中的委托:

   - (MKAnnotationView *)的MapView:(*的MKMapView)的MapView viewForAnnotation:(ID< MKAnnotation>){注解

您可以使用:

 ((MyLocation *)注释).identyfier

检查选择的注释(当然你也可以使用MyLocation类不同的变量)

I have a mapView where users press to drop a pin. There may be multiple pins at one time, and each annotation view has a callout that pushes a new view to the stack when it is pressed. What I want to do is pass the title of the annotation view to the label in the second view.

Here is the code where I drop the pin:

-(void)press:(UILongPressGestureRecognizer *)recognizer
{
    CGPoint touchPoint = [recognizer locationInView:worldView];
    CLLocationCoordinate2D touchMapCoordinate = [worldView convertPoint:touchPoint toCoordinateFromView:worldView];

    geocoder = [[CLGeocoder alloc]init];
    CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
                                                    altitude:CLLocationDistanceMax
                                          horizontalAccuracy:kCLLocationAccuracyBest
                                            verticalAccuracy:kCLLocationAccuracyBest
                                                   timestamp:[NSDate date]];
    [geocoder reverseGeocodeLocation:location
               completionHandler:^(NSArray *placemarks, NSError *error) {
                   NSLog(@"reverseGeocoder:completionHandler: called");
                   if (error) {
                       NSLog(@"Geocoder failed with error: %@", error);
                   } else {
                       CLPlacemark *place = [placemarks objectAtIndex:0];
                       address = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];

                       if (UIGestureRecognizerStateBegan == [recognizer state]) {
                           addressPin = [[MapPoint alloc]initWithCoordinate:touchMapCoordinate
                                                                        title:address];
                           [worldView addAnnotation:addressPin];
                       }
                   }
               }];
}

And here is the code where I call the second view:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
   PinViewController *pinViewController = [[PinViewController alloc]init];
    [self passValues];
    [[self navigationController]pushViewController:pinViewController animated:YES];
}

解决方案

You can override MKAnnotation (for example MyLocation)and declare in MyLocation.h file

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyLocation : NSObject <MKAnnotation> {
    NSNumber *identyfier;
    NSString *_name;
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (copy) NSNumber *identyfier;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *) identyfier;

@end

in MyLocation.m file:

#import "MyLocation.h"

@implementation MyLocation

@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
@synthesize identyfier = _identyfier;

- (id)initWithName:(NSString*)name address:(NSString*)address 
    coordinate:(CLLocationCoordinate2D)coordinate identyfier:(NSNumber *)identyfier {
    if ((self = [super init])) {
        _name = [name copy];
        _address = [address copy];
        _coordinate = coordinate;
        _identyfier = identyfier;
    }
    return self;
}

In your map view when you declare annotation use this methos:

MyLocation *pin = [[MyLocation alloc] initWithName:place.name address:place.address coordinate:coordinate2D identyfier:some_id];

So for example in your map delegate:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

you can use:

((MyLocation *)annotation).identyfier

to check selected annotation (of course you can use different variables in MyLocation class)

这篇关于MapView可以与标注多个批注,如何通过标题下一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆