如何使用iOS版Google Maps SDK设置setRegion? [英] How to setRegion with google maps sdk for iOS?

查看:113
本文介绍了如何使用iOS版Google Maps SDK设置setRegion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS的Google Maps sdk中设置setRegion? 我想为标记的位置和半径设置缩放比例.

How to setRegion with google maps sdk for iOS? I want set zoom for location and radius of markers.

推荐答案

更新:

下面的原始答案从SDK版本1.2开始已经过时-您现在可以使用GMSCameraUpdate类的fitBounds:方法:

The original answer below is obsolete as of version 1.2 of the SDK - you can now use the fitBounds: method of the GMSCameraUpdate class:

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update

原始答案:

MapKit中的MKMapPoint类型定义地图的2D投影.尽管投影的实际值是不透明的,但它们却等效于缩放级别20的像素.这可以用于将经/纬度值转换为像素,从而转换为缩放比例,从而转换为缩放比例.

The MKMapPoint type in MapKit defines a 2D projection of a map. Although the actual values of the projection are meant to be opaque, they turn out to be equivalent to pixels at zoom level 20. This can be used to convert lat/lon values to pixels, and therefore a scale, and therefore a zoom level.

首先定义两个位置,这些位置指定要显示的区域的边界.这些可能位于边界框的对角,或仅位于两个位置,例如:

Start by defining two locations which specify the bounds of the region you want to display. These could be opposite corners of the bounding box, or just two locations, for example:

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

如果要包含两个以上的点,则可以自己计算它们的范围.也可以使用GMSCoordinateBounds完成此操作,例如:

If you have more than two points that you want to include, you could calculate the bounds of them yourself. This can also be done using GMSCoordinateBounds, for example:

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;

接下来,您需要获取以磅为单位的地图视图的大小.您可以使用:

Next, you need to get the size of the map view in points. You could use this:

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

但这仅在您已经创建了地图视图的情况下才有效.另外,如果您使用的是入门指南中的示例代码,则将帧设置为CGRectZero,因为稍后将设置实际大小以填充屏幕.在这些情况下,如果要创建全屏地图,则可能需要这样的内容:

But that will only work if you've already created the map view. Also, if you're using the sample code in the getting started guide, the frame is set to CGRectZero, as the actual size will be set later to fill the screen. In these cases if you're creating a full-screen map then you might want something like this:

float mapViewWidth = [UIScreen mainScreen].applicationFrame.size.width;
float mapViewHeight = [UIScreen mainScreen].applicationFrame.size.height;

否则,请使用创建地图视图时使用的尺寸.

Otherwise, use the size which you're creating your map view with.

现在,您具有计算摄像机位置所需的信息:

Now you have the info necessary to calculate the camera position:

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];

然后您可以使用此摄像机初始化地图视图,或将地图视图设置为此摄像机.

You can then initialize the map view with this camera, or set the map view to this camera.

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

For this code to compile, you will need to add the MapKit framework to your project, and then also import it:

#import <MapKit/MapKit.h>

请注意,如果您的坐标跨越日期行,则此代码不处理环绕.例如,如果您尝试在东京和夏威夷使用此代码,而不是显示太平洋区域,它将尝试显示几乎整个世界.在纵向模式下,无法缩小到足以看到左侧的夏威夷和右侧的东京的位置,因此地图最终以非洲为中心,而这两个位置均不可见.如果需要,您可以修改上面的代码以在日期行处处理换行.

Note that this code doesn't handle wrap-around if your coordinates span across the date line. For example if you tried using this code with Tokyo and Hawaii, instead of displaying an area of the Pacific, it will try to display almost the entire world. In portrait mode it's not possible to zoom out far enough to see Hawaii on the left and Tokyo on the right, and so the map ends up centred on Africa with neither location visible. You could modify the above code to handle the wrap-around at the date line if you wanted to.

这篇关于如何使用iOS版Google Maps SDK设置setRegion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆