gpt4 book ai didi

ios - 如何在Google Map,Swift中设置受限区域或受限区域

转载 作者:行者123 更新时间:2023-11-28 14:57:09 26 4
gpt4 key购买 nike

我的 Googlemap 上有标记列表,修复了所有标记。现在我需要获取 Limited Region only possible to scroll inside the limited Region。这怎么可能。任何人都请帮我修复它。

这是我的中心位置

 let center = CLLocationCoordinate2DMake(11.250220, 75.781573)


let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 18.0)

最佳答案

首先定义两个位置,这两个位置指定了您要显示的区域的边界。这些可以是边界框的对角,或者只是两个位置,例如:

CLLocationCoordinate2D location1 = 
CLLocationCoordinate2DMake(-33.8683, 151.2086); // Sydney
CLLocationCoordinate2D location2 =
CLLocationCoordinate2DMake(-31.9554, 115.8585); // Perth

如果您要包括两个以上的点,您可以自己计算它们的边界。这也可以使用 GMSCoordinateBounds 来完成,例如:

GMSCoordinateBounds* bounds =
[[GMSCoordinateBounds alloc]
initWithCoordinate: CLLocationCoordinate2DMake(-33.8683, 151.2086) // Sydney
andCoordinate: CLLocationCoordinate2DMake(-31.9554, 115.8585)]; // Perth
bounds = [bounds including:
CLLocationCoordinate2DMake(-12.4667, 130.8333)]; // Darwin
CLLocationCoordinate2D location1 = bounds.southWest;
CLLocationCoordinate2D location2 = bounds.northEast;

接下来,您需要获取 map View 的大小(以磅为单位)。你可以使用这个:

float mapViewWidth = _mapView.frame.size.width;
float mapViewHeight = _mapView.frame.size.height;

现在您有了计算相机位置所需的信息:

MKMapPoint point1 = MKMapPointForCoordinate(location1);
MKMapPoint point2 = MKMapPointForCoordinate(location2);
MKMapPoint centrePoint = MKMapPointMake(
(point1.x + point2.x) / 2,
(point1.y + point2.y) / 2);
CLLocationCoordinate2D centreLocation = MKCoordinateForMapPoint(centrePoint);

double mapScaleWidth = mapViewWidth / fabs(point2.x - point1.x);
double mapScaleHeight = mapViewHeight / fabs(point2.y - point1.y);
double mapScale = MIN(mapScaleWidth, mapScaleHeight);

double zoomLevel = 20 + log2(mapScale);

GMSCameraPosition *camera = [GMSCameraPosition
cameraWithLatitude: centreLocation.latitude
longitude: centreLocation.longitude
zoom: zoomLevel];

然后您可以使用此相机初始化 map View ,或将 map View 设置为此相机。

要编译此代码,您需要将 MapKit 框架添加到您的项目中,然后再导入它:

#import <MapKit/MapKit.h>

请注意,如果您的坐标跨越日期变更线,则此代码不会处理回绕。例如,如果您尝试将此代码用于东京和夏威夷,它不会显示太平洋区域,而是会尝试显示几乎整个世界。在纵向模式下,无法缩小到左侧看到夏威夷和右侧看到东京,因此 map 最终以非洲为中心,两个位置都不可见。如果需要,您可以修改上述代码以处理日期变更线处的回绕。

关于ios - 如何在Google Map,Swift中设置受限区域或受限区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49229362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com