【问题标题】:How to add a Map annotation on MKMapView?如何在 MKMapView 上添加地图注释?
【发布时间】:2011-03-30 20:15:31
【问题描述】:

嘿,我想在我的地图上添加注释。我该怎么做?

这是我的代码:

- (void)abreMapa:(NSString *)endereco {

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [endereco stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    //Show error
}

CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
myMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);

[self.view addSubview:mapa];


 }

谢谢!

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    由于 MKAnnotation 是一个协议,您必须定义自己的类来实现该协议。例如,

    @interface SPAnnotation : NSObject <MKAnnotation> {
        CLLocationCoordinate2D coordinate;
    }
    
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    
    - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
    

    如果您有要映射的位置的纬度、经度:

    SPAnnotation *annotation = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
    [myMap addAnnotation:annotation];
    

    【讨论】:

    • 我得到“使用未声明的标识符'注释'。
    • 你必须定义你自己的 Annotation 类,因为 MKAnnotation 是一个协议。看到这个答案:stackoverflow.com/questions/2878758/iphone-create-mkannotation
    • 也可以使用预定义的MKPointAnnotation类。
    • aBitObvious,我该怎么做?有教程吗?
    • @Lucas - 只需分配并初始化一个 MKPointAnnotation。然后,在新创建的 MKPointAnnotation 上调用 setCoordinate、setTitle 和 setSubtitle(根据需要)。最后,[myMap addAnnotation:theNewlyCreatedAnnotation];
    【解决方案2】:

    您还必须编写一个采用 MKAnnotation 协议的注释类。例如:

    @interface MyMapAnnotation : NSObject <MKAnnotation> {
    ...
    }
    

    【讨论】:

    • 你的意思是在同一个标​​题中做?
    • 您将需要为您的注释创建一个新类,它有自己的头文件和主文件。至少,此类将需要实现 - (id) initWithCoords:(CLLocationCoordinate2D) coords 和 CLLocationCoordinate2D 坐标属性。见:MKAnnotation docs
    【解决方案3】:

    试试这个

    Pin *selected = [[Pin alloc] init];
    
    for(i=0; i<[[newxml dataarray] count]; i++)
    {
        gapi = [newxml.dataarray objectAtIndex:i]; 
        MKCoordinateRegion region ;  // = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
        region.center. latitude = [gapi.lat floatValue];
        region.center. longitude = [gapi.lng floatValue];
    
        region.span.longitudeDelta = 0.05f;
        region.span.latitudeDelta = 0.05f;
    
        [self.mapview setRegion:region animated:YES]; 
    
        Pin* myAnnotation1=[[Pin alloc] init];
    
        myAnnotation1.coordinate=region.center;;
        myAnnotation1.title=gapi.name;
        myAnnotation1.itemIndex = i;
        myAnnotation1.subtitle=gapi.address;
        [self.mapview addAnnotation:myAnnotation1];
    
        if (j == i)
        {  
                selected = myAnnotation1;
        }
    
        if(k==123)
        {
            [self.mapview selectAnnotation:selected animated:NO]; 
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多