MKPlacemark引脚标题

时间:2011-10-21 19:14:31

标签: ios uikit mkmapview title mkplacemark

我的mapview工作正常,但放在地图上的图钉标题为美国。我该如何更改此标题?

    MKCoordinateRegion thisRegion = {{0.0,0.0}, {0.0,0.0}};

        thisRegion.center.latitude = 22.569722;
        thisRegion.center.longitude = 88.369722;

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = 22.569722;
        coordinate.longitude = 88.369722;

        thisRegion.center = coordinate;

        MKPlacemark *mPlacemark = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease];

        [mapView addAnnotation:mPlacemark];
        [mapView setRegion:thisRegion animated:YES];

3 个答案:

答案 0 :(得分:14)

相当古老的问题,但也许其他人偶然发现同样的问题(就像我一样):

不要将MKPlacemark添加到地图的注释中;请改用MKPointAnnotation。此类具有不是只读的标题和副标题属性。设置它们时,地图上的注释会相应更新 - 这可能就是您想要的。

要在代码中使用MKPointAnnotation,请使用以下代码替换分配和添加MKPlacemark的行:

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = NSLocalizedString(@"Dropped Pin",
                                     @"Title of a dropped pin in a map");
[mapView addAnnotation:annotation];

您也可以在以后随时设置标题和字幕属性。例如,如果您正在运行异步地址查询,则可以在地址可用时立即将副标题设置为注释的地址。

答案 1 :(得分:5)

下面的代码演示如何使用iOS 5.1中的CLGeocoder在地图上放置注释

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

// Apple recommendation - if location is older than 30s ignore
// Comment out below during development
/*  if (fabs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]]) > 30) {
    NSLog(@"timestamp");
    return;
}*/   

CLLocation *coord = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];                            
[geocoder reverseGeocodeLocation:coord completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) {
        NSLog(@"Geocode failed with error");
    }

    // check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topresult = [placemarks objectAtIndex:0];
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        annotation.coordinate = locationManager.location.coordinate;
        annotation.title = NSLocalizedString(@"You are here", @"Title");
        annotation.subtitle = [NSString stringWithFormat:@"%@, %@", [topresult subAdministrativeArea], [topresult locality]];
        [self.mapView addAnnotation:annotation];
    }
}];
}

答案 2 :(得分:0)