[伙计们,我正在开发Map-view Base应用程序。我几乎已经完成了地图视图的所有操作,但是无法比较两个不同的地图视图区域并找到新的地图视图区域。例如,如果用户拖动地图,我想查找该区域已更改了多少。
此答案显示了如何比较两个地图点。您可以在地图区域的中心使用它:link。
部分答案。平等遵守MKCoordinateRegion
extension MKCoordinateRegion: Equatable
{
public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool
{
if lhs.center.latitude != rhs.center.latitude || lhs.center.longitude != rhs.center.longitude
{
return false
}
if lhs.span.latitudeDelta != rhs.span.latitudeDelta || lhs.span.longitudeDelta != rhs.span.longitudeDelta
{
return false
}
return true
}
}
首先,您需要找到初始地图区域。假设您的地图名为mapView ...,您可以首先在(viewDidLoad)中找到它:
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationDegrees lat = center.latitude;
CLLocationDegrees lon = center.longitude;
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span = region.span;
//Assuming they have been declared as instance variables of type double
current_lat_low = lat - span.latitudeDelta / 2.0;
current_lat_high = lat + span.latitudeDelta / 2.0;
current_lon_low = lon - span.longitudeDelta / 2.0;
current_lon_high = lon + span.longitudeDelta / 2.0;
这将为您显示地图的初始区域。然后在
- (void)mapView:(MKMapView*)mapView regionDidChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationDegrees lat = center.latitude;
CLLocationDegrees lon = center.longitude;
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span = region.span;
double lat_low = lat - span.latitudeDelta / 2.0;
double lat_high = lat + span.latitudeDelta / 2.0;
double lon_low = lon - span.longitudeDelta / 2.0;
double lon_high = lon + span.longitudeDelta / 2.0;
//do any work comparing the initial lat/lons with the new values
.....
//set current lat/lon to be the new lat/lon after work is complete
current_lat_low = lat_low;
current_lat_high = lat_high;
current_lon_low = lon_low;
current_lon_high = lon_high;
}