我想在mapView中显示位置名称和引脚点。我试过很多方法,但没有一个能帮助我。我被困在这个位置。 我正在使用这段代码:
-(void)getMe:(id)sender
{
[self.view endEditing:YES];
NSString *addresss = userText.text;
NSString *esc_addr = [addresss stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat: @"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];
NSDictionary *googleResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *resultsDict = [googleResponse valueForKey: @"results"]; // get the results dictionary
NSDictionary *geometryDict = [ resultsDict valueForKey: @"geometry"]; // geometry dictionary within the results dictionary
NSDictionary *locationDict = [ geometryDict valueForKey: @"location"]; // location dictionary within the geometry dictionary
// nslo (@”– returning latitude & longitude from google –”);
NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject]; // (one element) array entries provided by the json parser
NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject]; // (one element) array entries provided by the json parser
myLocation.latitude = [latString doubleValue]; // the json parser uses NSArrays which don’t support “doubleValue”
myLocation.longitude = [lngString doubleValue];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = myLocation.latitude;
zoomLocation.longitude=myLocation.longitude;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1500, 0.5*1500);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = myLocation.latitude;
coordinates.longitude=myLocation.longitude;
point.coordinate =coordinates;
point.title = @"My Location";
point.subtitle = @"Reached";
[mp addAnnotation:point];
// 3
[mp setRegion:viewRegion animated:YES];
}
mp是我的mapView Outlet。我正在考虑这个输出:
但是我想要带针的位置标题。我该怎么做才能实现这一目标。
答案 0 :(得分:1)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
}