【发布时间】:2015-05-11 10:24:45
【问题描述】:
我刚从 Apple Maps 切换到 Google Maps。我似乎找不到答案的一个问题是,如何让 GMSMarker 的图标从中心开始,而不是从图像底部开始。
我的意思的一个例子是当前位置点图标以它要表达的坐标为中心开始。但是 GMSMarkers 图标从图标底部开始。
【问题讨论】:
标签: ios google-maps google-maps-markers center gmsmapview
我刚从 Apple Maps 切换到 Google Maps。我似乎找不到答案的一个问题是,如何让 GMSMarker 的图标从中心开始,而不是从图像底部开始。
我的意思的一个例子是当前位置点图标以它要表达的坐标为中心开始。但是 GMSMarkers 图标从图标底部开始。
【问题讨论】:
标签: ios google-maps google-maps-markers center gmsmapview
您可以使用属性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_;
【讨论】:
yourMarker.groundAnchor = CGPoint(0.5, 0.5); 谢谢@adboco!
在仔细阅读了 Google 地图文档后,我想出了如何做到这一点。我相信这就是它的意图。
UIImage *markerIcon = [UIImage imageNamed:@"markericon.png"];
markerIcon = [markerIcon imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, (markerIcon.size.height/2), 0)];
self.marker.icon = markerIcon;
【讨论】:
在 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
}
【讨论】: