-(CLLocationCoordinate2D)getGeoCoding:(NSString *)address
{
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@",address];
NSURL *url = [NSURL URLWithString:req];
NSError *error;
//NSURLResponse *response;
CLLocationCoordinate2D findlocation;
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:Nil error:&error];
if(error)
{
NSLog(@"%@",[error localizedDescription]);
findlocation.latitude=0.0;
findlocation.longitude =0.0;
}
else
{
NSArray *results = (NSArray *)responseDictionary[@"results"];
NSDictionary *firstitem = (NSDictionary *)[results objectAtIndex:0];
NSLog(@"%@",firstitem);
NSDictionary *geometry = (NSDictionary *)[firstitem valueForKey:@"geometry"];
NSLog(@"%@",geometry);
NSDictionary *location = (NSDictionary *)[geometry valueForKey:@"location"];
NSLog(@"%@",location);
NSNumber *lat = (NSNumber *)[location valueForKey:@"lat"];
NSLog(@"%@",lat);
NSNumber *lng = (NSNumber *)[location valueForKey:@"lng"];
NSLog(@"%@",lng);
findlocation.latitude = [lat doubleValue];
findlocation.longitude = [lng doubleValue];
}
return findlocation;
}
我从上面的代码中获取位置。
-(void)getLocation:(id)sender
{
NSString *str = self.textView.text;
CLLocationCoordinate2D location;
location=[self getGeoCoding:str];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.mapView.showsUserLocation =YES;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.3*METERS_PER_MILES, 0.3*METERS_PER_MILES);
[self.locationManager startUpdatingLocation];
[self.mapView reloadInputViews];
[self.mapView setRegion:viewRegion animated:YES];
}
位置代码在上述功能之后。 函数被调用,但在iPhone模拟器中不起作用..? 如果设置任何设置请告诉我代码中的问题因为iPhone模拟器地图没有从当前位置移动.. 可能吗?怎么样?