【问题标题】:GMSMarker icon from center (iOS)中心的 GMSMarker 图标 (iOS)
【发布时间】:2015-05-11 10:24:45
【问题描述】:

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

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

【问题讨论】:

    标签: ios google-maps google-maps-markers center gmsmapview


    【解决方案1】:

    您可以使用属性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_;
    

    【讨论】:

    • 救了我一天的兄弟。谢谢+1
    • 谢谢兄弟。不错的答案
    • 对于那些只想移动标记位置的人,您只需要yourMarker.groundAnchor = CGPoint(0.5, 0.5); 谢谢@adboco!
    • 非常感谢您的简洁解释
    • 非常感谢您的简洁解释。
    【解决方案2】:

    在仔细阅读了 Google 地图文档后,我想出了如何做到这一点。我相信这就是它的意图。

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

    【讨论】:

    • 我试过这个方法,它看起来很有效,但它会导致一些意想不到的副作用......这样做不会移动可点击区域所以原始标记之外的区域rect 将不可点击。
    • 我试过了,但没有用。但是这个问题可以使用 Marker.groundAnchor = CGPoint(0.5, 0.5);对于 obj c 和 marker.groundAnchor = CGPoint(x: 0.5, y: 0.5) 对于 swift
    【解决方案3】:

    在 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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多