12

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

    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];
4

3 回答 3

15

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

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

要在您的代码中使用 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];

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

于 2012-10-31T08:55:31.850 回答
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];
    }
}];
}
于 2012-12-07T21:52:33.697 回答