在我的地图应用程序中,用户可以在搜索栏中输入地址,该位置会以红色图钉显示。是否可以在我的代码中让用户在将图钉放置在地图上之前输入自定义标题和副标题?标题和副标题应显示在标注气泡中。这是怎么做到的?现在,我所有的别针标题都是“我的地方”。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
if (!self.geocoder)
{
self.geocoder = [[CLGeocoder alloc] init];
}
NSString *address = [NSString stringWithFormat:@"%@", self.searchBar.text];
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog (@"%f %f", coordinate.latitude, coordinate.longitude);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
region.center = coordinate;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
[annotation setTitle:@"My Place"];
[[self mapView] addAnnotation:annotation];
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];
[self.searchBar resignFirstResponder];
self.searchBar.text = @"";
}
}];
}
另外,如何将我所有的 pin 保存到 NSUserDefault,以便在应用程序重新启动时显示它们?这会起作用吗,还是只会节省一个引脚?
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setDouble:coordinate.latitude forKey:allPins_latitude];
[defaults setDouble:coordinate.longitude forKey:allPins_longitude];
[defaults setBool:YES forKey:allPins_coordinates];
[defaults synchronize];
我将不胜感激任何帮助!谢谢!