中心的GMSMarker图标(iOS)

Nic*_*ata 31 google-maps center google-maps-markers ios gmsmapview

我刚从Apple Maps切换到Google Maps.我似乎无法找到答案的问题是如何使GMSMarker的图标从中心开始,而不是从图像的底部开始.

我的意思是当前位置点图标以其要表达的坐标为中心开始.但是,GMSMarkers图标从图标的底部开始.

adb*_*oco 57

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

Google Maps SDK for iOS文档:

地锚指定图标图像中锚定到地球表面上标记位置的点.此点在连续空间[0.0,1.0] x [0.0,1.0]内指定,其中(0,0)是图像的左上角,(1,1)是右下角.

示例:

以下示例将标记旋转90°.将groundAnchor属性设置为0.5,0.5会导致标记绕其中心而不是其基部旋转.

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_;
Run Code Online (Sandbox Code Playgroud)

  • 对于那些只想移动标记位置的人,您只需要“yourMarker.groundAnchor = CGPoint(0.5, 0.5);”谢谢@adboco! (2认同)

Nic*_*ata 7

我非常仔细地阅读了Google地图文档后想出了如何做到这一点.我相信这就是它的目的.

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


Bij*_*wat 5

在 Swift 5 中

    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
    }
Run Code Online (Sandbox Code Playgroud)