iOS谷歌地图SDK GMSMarker定位

13

我正在使用谷歌地图,并且我能够通过使用以下方式将地图居中于GMSMarker:

I am working with Google maps and I am able to center the map to the GMSMarker by using

 GMSCameraPosition *camera =
    [[GMSCameraPosition alloc] initWithTarget:marker.position
                                         zoom:MAP_ZOOM_LEVEL
                                      bearing:0
                                 viewingAngle:0];
    [mapView animateToCameraPosition:camera];

我展示了一个大小为200*150的自定义标注,当摄像机位置改变时,它的一部分被隐藏,但我希望标注位于中心,地图点位于其下方。有什么建议吗?

2个回答

18

可以查看使用GMSProjection。要将地图中心从标记位置偏移100像素,可以执行以下操作:

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
  CGPoint point = [mapView_.projection pointForCoordinate:marker.position];
  point.x = point.x + 100;
  GMSCameraUpdate *camera =
      [GMSCameraUpdate setTarget:[mapView_.projection coordinateForPoint:point]];
  [mapView_ animateWithCameraUpdate:camera];
  return YES;
}

1
我尝试过这个,但是地图会以标记为中心点自动居中。 - Satheesh
是的,这就是我编写的代码所做的。您需要修改point.x和point.y以成为您想要的任何偏移量。 - skarE
我把 x 和 y 都加了1000,仍然发生同样的事情。 - Satheesh
是的,我已经正确地设置了它,并且它也被调用了。 - Satheesh
1
遇到了相同的问题。我在这个方法的末尾返回false,请检查是否返回true。然后上面的解决方案就可以完美地工作了! - Alexander
显示剩余2条评论

5

Swift 4, 3

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    var point:CGPoint = mapView.projection.point(for: marker.position)
    point.x = point.x + 100

    let camera:GMSCameraUpdate = GMSCameraUpdate.setTarget(mapView.projection.coordinate(for: point))
    mapView.animate(with: camera)

    return true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接