我正在使用MapKit和CoreLocation查找用户位置,然后将其显示在地图上。
我使用此代码显示城市和州(这是我感兴趣的,而不是确切的位置)。
// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:self.locationManager.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = placemark.region.center.latitude;
zoomLocation.longitude= placemark.region.center.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE * 2, METERS_PER_MILE * 2);
[self.mapView setRegion:viewRegion animated:NO];
NSString *location = [NSString stringWithFormat:@"%@, %@", placemark.locality, placemark.administrativeArea];
NSLog(@"%@", location);
}];
}
这很好用,但我真正想做的是放大到城市中心,而不是用户所在位置。
我可以使用某种属性来执行此操作吗?
否则,是否有人知道某项工作?那太好了,谢谢!
答案 0 :(得分:4)
不直接。 没有 api就像MKGetClosestCapitalTo(userLocation)
BUT
您可以使用用户的位置进行反向地理编码,然后使用城市名称对地址进行地理编码=>那会让你成为市中心。
这样:
(你已经有1只做2和3;)
答案 1 :(得分:1)
我会建议Daij-Djan做两个地理编码的方法,在查询中基本上“失去解决方案”。例如,如果您发现用户位于德克萨斯州奥斯汀的某个地址,那么您将从结果中获得“Austin,TX”,并要求的坐标。那么你将拥有Apple认为奥斯汀的中心。
另一种方法可能是绑定到外部数据库或API。例如,维基百科的每个城市都有一个中心坐标,您可以查询该城市。使用。
答案 2 :(得分:-1)
如果您想以特定城市为中心,您可以在以下代码中指定城市的坐标,地图视图将以其为中心。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance("SPECIFY THE COORDINATE OF THE CITY YOU WANT TO CENTER AROUND HERE", 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];