简体   繁体   English

更改Google Maps iOS SDK的默认位置

[英]change the default location on google maps iOS sdk

it is my first time to use google map and i asked to let the user choose the location that he wanted to point by taping on the map. 这是我第一次使用Google地图,我要求让用户通过点击地图来选择他想指向的位置。 when the application start it will point on the device location how the user will change it to the location he wants? 当应用程序启动时,它将指向设备位置,用户将如何将其更改为所需的位置?

First get the point of user's tap. 首先了解用户的点击位置。 Then, get coordinate value for that CGPoint and set it as mapView's center. 然后,获取该CGPoint的坐标值并将其设置为mapView的中心。

-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    // Get tap point
    CGPoint tapPoint = [recognizer locationInView:[recognizer superView]];

    // Convert CGPoint to CLLocationCoordinate2D
    CLLocationCoordinate2D center = [self.mapView.projection coordinateForPoint:tapPoint];

    // Set camera of mapView
    GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:center.latitude longitude:center.longitude zoom:self.mapView.camera.zoom];
    [self.mapView setCamera:camera];
}

或者,您可以实现GMSMapViewDelegate并使用- mapView:didTapAtCoordinate:方法。

You can move the view port by doing something similar like this to move the center point of map view 您可以通过执行类似的操作来移动视图端口,以移动地图视图的中心点

GMSCameraPosition *sydney = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
[mapView_ setCamera:sydney];

(Google Map SDK called it camera position, as if you use a camera to target a portion of the whole map) (Google Map SDK将其称为相机位置,就好像您使用相机来定位整个地图的一部分一样)

Checkout https://developers.google.com/maps/documentation/ios/views?hl=en Moving the camera section 结帐https://developers.google.com/maps/documentation/ios/views?hl=zh-CN 移动摄像头部分

So here, in order to finish your task with move use to the point where he tapped. 所以在这里,为了完成您的任务,请移动到他点击的位置。 You'll need to do following steps: 您需要执行以下步骤:

  1. define a iVar or property to keep record of the tap point location(lat/lng): 定义一个iVar或属性以记录分接点位置(纬度/经度):

    CLLocationCoordinate2D *currentTapLocation; CLLocationCoordinate2D * currentTapLocation;

  2. In didTapAtCoordinate delegate, fetch that position 在didTapAtCoordinate委托中,获取该位置

     - (void) mapView:(GMSMapView *) mapView didTapAtCoordinate:(CLLocationCoordinate2D) coordinate{ currentTapLocation = coordinate; } 
  3. Create a GMSCamera and set camera to currentTapLocation 创建一个GMSCamera并将摄像机设置为currentTapLocation

     - (void) mapView:(GMSMapView *) mapView didTapAtCoordinate:(CLLocationCoordinate2D) coordinate{ currentTapLocation = coordinate; GMSCameraPosition *newCameraPosition = [GMSCameraPosition cameraWithLatitude:currentTapLocation.latitude longitude:currentTapLocation.longitude zoom:6]; [mapView_ setCamera:newCameraPosition]; } 
  4. Optional step: if you want animate to the new position instead setCamera directly, you can do: 可选步骤:如果要设置动画到新位置,而不是直接设置setCamera,则可以执行以下操作:

    [mapView_ animateWithCameraUpdate:[mapView_ setCamera:newCameraPosition];]; [mapView_ animateWithCameraUpdate:[mapView_ setCamera:newCameraPosition];];

    instead of 代替

    [mapView_ setCamera:newCameraPosition]; [mapView_ setCamera:newCameraPosition];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM