Ios 如何在两个坐标内显示地图?
我想显示两个坐标之间的地图。我使用了这段代码来显示包含两个坐标的地图矩形。但我的两个坐标都没有显示,而是显示了两个坐标之间的区域,不包括坐标。 我应该怎么做才能包括我的两个坐标呢Ios 如何在两个坐标内显示地图?,ios,swift,mapkit,coordinates,cllocationmanager,Ios,Swift,Mapkit,Coordinates,Cllocationmanager,我想显示两个坐标之间的地图。我使用了这段代码来显示包含两个坐标的地图矩形。但我的两个坐标都没有显示,而是显示了两个坐标之间的区域,不包括坐标。 我应该怎么做才能包括我的两个坐标呢 let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39) let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69) // convert them to MKMapPoint
let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
// convert them to MKMapPoint
let p1 = MKMapPointForCoordinate (coordinate2);
let p2 = MKMapPointForCoordinate (coordinate1);
let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
mapView.setVisibleMapRect(mapRect, animated: true)
您可以尝试以下功能,使通过的点在地图中居中:
func centerLocationInMapView(centerPoint: CLLocationCoordinate2D) {
floatForRadiusInMiles = 10.0 // you can ignore this i have taken this for my custom radius property
var scalingFactor: Double = abs((cos(2 * M_PI * centerPoint.latitude / 360.0)))
var coordinateSpan: MKCoordinateSpan = MKCoordinateSpanMake(floatForRadiusInMiles / 69.0, floatForRadiusInMiles / (scalingFactor * 69.0))
var coordinateRegion: MKCoordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan)
self.mapViewForHomeScreen.scrollEnabled = true
mapViewForHomeScreen.setRegion(coordinateRegion, animated: TRUE)
mapViewForHomeScreen.regionThatFits(coordinateRegion)
}
根据需要调用此函数
self.centerLocationInMapView( (first latitude + second latitude)/2 , (first longitude + second longitude)/2 )
通过两个lat之间的中心点,使中心位于地图上
中锋
您可以尝试以下功能,使通过的点在地图中居中:
func centerLocationInMapView(centerPoint: CLLocationCoordinate2D) {
floatForRadiusInMiles = 10.0 // you can ignore this i have taken this for my custom radius property
var scalingFactor: Double = abs((cos(2 * M_PI * centerPoint.latitude / 360.0)))
var coordinateSpan: MKCoordinateSpan = MKCoordinateSpanMake(floatForRadiusInMiles / 69.0, floatForRadiusInMiles / (scalingFactor * 69.0))
var coordinateRegion: MKCoordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan)
self.mapViewForHomeScreen.scrollEnabled = true
mapViewForHomeScreen.setRegion(coordinateRegion, animated: TRUE)
mapViewForHomeScreen.regionThatFits(coordinateRegion)
}
根据需要调用此函数
self.centerLocationInMapView( (first latitude + second latitude)/2 , (first longitude + second longitude)/2 )
通过两个lat之间的中心点,使中心位于地图上
中锋
基本上这是由于自动布局。我们应该使用
mapViewDidFinishLoadingMap(mapView:MKMapView)
方法来设置它
func mapViewDidFinishLoadingMap(mapView: MKMapView) {
// this is where visible maprect should be set
let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
// convert them to MKMapPoint
let p1 = MKMapPointForCoordinate (coordinate2);
let p2 = MKMapPointForCoordinate (coordinate1);
let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
mapView.setVisibleMapRect(mapRect, animated: true)
}
基本上这是由于自动布局。我们应该使用
mapViewDidFinishLoadingMap(mapView:MKMapView)
方法来设置它
func mapViewDidFinishLoadingMap(mapView: MKMapView) {
// this is where visible maprect should be set
let coordinate1 = CLLocationCoordinate2DMake(28.53, 77.39)
let coordinate2 = CLLocationCoordinate2DMake(29.13,76.69)
// convert them to MKMapPoint
let p1 = MKMapPointForCoordinate (coordinate2);
let p2 = MKMapPointForCoordinate (coordinate1);
let mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));
mapView.setVisibleMapRect(mapRect, animated: true)
}
使用这一点,我肯定得到了中心点,但地图上仍然显示了中心点周围的区域,而不是坐标。如果我增加半径,它会显示两个坐标,但我希望半径是动态的。你在相应的坐标上添加了销吗?实际上没有销。我只需要在这两个坐标之间绘制一个矩形地图。在这些坐标中添加图钉,并调整地图缩放级别,以便在地图角处调整图钉,然后在获得相应的矩形后删除图钉。。!使用这一点,我肯定得到了中心点,但地图上仍然显示了中心点周围的区域,而不是坐标。如果我增加半径,它会显示两个坐标,但我希望半径是动态的。你在相应的坐标上添加了销吗?实际上没有销。我只需要在这两个坐标之间绘制一个矩形地图。在这些坐标中添加图钉,并调整地图缩放级别,以便在地图角处调整图钉,然后在获得相应的矩形后删除图钉。。!