2011-10-21 24 views
10

Tôi có chế độ xem bản đồ của tôi hoạt động tốt, nhưng ghim được đặt trên bản đồ có tiêu đề Hoa Kỳ. Làm thế nào tôi có thể thay đổi tiêu đề này?Tiêu đề ghim MKPlacemark

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

Trả lời

13

câu hỏi Khá cũ, nhưng có lẽ ai đó tình cờ khi cùng một vấn đề (như tôi đã làm):

Đừng thêm một MKPlacemark để chú thích của bản đồ; thay vào đó hãy sử dụng MKPointAnnotation. Lớp này có các thuộc tính tiêu đề và phụ đề không phải chỉ đọc. Khi bạn đặt chúng, chú thích trên bản đồ được cập nhật tương ứng - và đây có thể là điều bạn muốn.

Để sử dụng MKPointAnnotation trong mã của bạn, thay thế các dòng phân bổ và thêm MKPlacemark với mã này:

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

Bạn có thể thiết lập các tiêu đề và phụ đề tính bất cứ lúc nào sau đó, quá. Ví dụ: nếu bạn có truy vấn địa chỉ không đồng bộ đang chạy, bạn có thể đặt phụ đề vào địa chỉ của chú thích ngay sau khi địa chỉ có sẵn.

5

Mã dưới đây giải thích cách đặt một chú thích trên bản đồ bằng CLGeocoder trong iOS 5.1

-(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]; 
    } 
}]; 
}