中心的 GMSMarker 图标 (iOS)

GMSMarker icon from center (iOS)

我刚从 Apple 地图切换到 Google 地图。我似乎找不到答案的一个问题是如何使 GMSMarker 的图标从中心开始而不是从图像底部开始。

我的意思的一个例子是当前位置点图标以它要表达的坐标为中心开始。然而 GMSMarkers 图标从图标的底部开始。

在仔细阅读 Google 地图文档后,我明白了如何去做。我相信这就是它的目的。

UIImage *markerIcon = [UIImage imageNamed:@"markericon.png"];
markerIcon = [markerIcon imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, (markerIcon.size.height/2), 0)];
self.marker.icon = markerIcon;

您可以使用 属性 groundAnchor 更改标记图标的起始位置。

Google Maps SDK iOS 文档:

The ground anchor specifies the point in the icon image that is anchored to the marker's position on the Earth's surface. This point is specified within the continuous space [0.0, 1.0] x [0.0, 1.0], where (0,0) is the top-left corner of the image, and (1,1) is the bottom-right corner.

示例

The below example rotates the marker 90°. Setting the groundAnchor property to 0.5,0.5 causes the marker to be rotated around its center, instead of its base.

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
CLLocationDegrees degrees = 90;
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.groundAnchor = CGPointMake(0.5, 0.5);
london.rotation = degrees;
london.map = mapView_;

在Swift5

    let marker: GMSMarker = GMSMarker() // Allocating Marker
    marker.title = "Your location" // Setting title
    marker.snippet = "Sub title" // Setting sub title
    marker.icon = UIImage(named: "radio") // Marker icon
    marker.appearAnimation = .pop // Appearing animation. default
    marker.position = CLLocationCoordinate2D.init(latitude: 26.8289443, longitude: 75.8056178)
    
    marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)  // this is the answer of this question
            
    DispatchQueue.main.async { // Setting marker on mapview in main thread.
        marker.map = self.googleMapView // Setting marker on Mapview
    }