【问题标题】:How Can I Draw A Radius Around A Map Marker Using Google Maps SDK (Swift/iOS)?如何使用 Google Maps SDK (Swift/iOS) 在地图标记周围绘制半径?
【发布时间】:2015-03-12 05:47:11
【问题描述】:

用户可以通过两种方式在应用程序内的地图上放置标记 - 通过地图上方的 UISearchBar(尚未完成),以及长按地图上他们希望标记出现的位置。我为标记使用了一个全局变量,因为用户只能在地图上设置一个标记。每当创建标记时,我想在标记周围绘制一个半径(圆)。到目前为止,这是我的代码:

var mapMarker = GMSMarker()
...

 //This function dectects a long press on the map and places a marker at the coordinates of the long press.
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {

        //Set variable to latitude of didLongPressAtCoordinate
        var latitude = coordinate.latitude

        //Set variable to longitude of didLongPressAtCoordinate
        var longitude = coordinate.longitude

        //Feed position to mapMarker
        mapMarker.position = CLLocationCoordinate2DMake(latitude, longitude)

        //Define attributes of the mapMarker.
        mapMarker.icon = UIImage(named: "mapmarkericon")

        //Set icon anchor point
        mapMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)

        //Enable animation on mapMarker
        mapMarker.appearAnimation = kGMSMarkerAnimationPop

        //Display the mapMarker on the mapView.
        mapMarker.map = mapView

        drawCircle(coordinate)

        }

func drawCircle(position: CLLocationCoordinate2D) {

    //var latitude = position.latitude
    //var longitude = position.longitude
    //var circleCenter = CLLocationCoordinate2DMake(latitude, longitude)
    var circle = GMSCircle(position: position, radius: 3000)
    circle.strokeColor = UIColor.blueColor()
    circle.fillColor = UIColor(red: 0, green: 0, blue: 0.35, alpha: 0.05)
    circle.map = mapView

}

诚然,这是我的第一个 iOS/Swift 应用程序。我想如果我将坐标从 didLongPressAtCoordinate 传递给 drawCircle() 函数,但显然我做错了什么。我整天都在寻找帮助,但只找到了适用于 Android 和 Google Maps API v3 的东西。谢谢!

编辑 我的代码确实有效,但半径无法缩放,并且图标出现在图标上方。当我放大地图时,我能够看到半径。存在三个问题:

  1. 图标不会根据地图的缩放级别进行缩放。
  2. 半径不会根据地图的缩放级别进行缩放。
  3. 如果标记位置更新,半径不会随之移动。相反,绘制了一个新的半径......所以地图上有两个半径。

【问题讨论】:

  • 你能描述一下它是怎么出错的,和/或张贴截图吗?
  • 我已经更新了我的帖子。显然我的代码确实有效,只是不完全符合我的预期。我需要解决 3 个问题。

标签: ios swift google-maps-sdk-ios


【解决方案1】:

这是在指定半径上以英里为单位绘制圆的快速代码

let circleCenter : CLLocationCoordinate2D  = CLLocationCoordinate2DMake(centerLattitude, centerLongitude);
let circ = GMSCircle(position: circleCenter, radius: distanceInMile * 1609.34)
circ.fillColor = UIColor(red: 0.0, green: 0.7, blue: 0, alpha: 0.1)
circ.strokeColor = UIColor(red: 255/255, green: 153/255, blue: 51/255, alpha: 0.5)
circ.strokeWidth = 2.5;
circ.map = self.googleMapView;

【讨论】:

    【解决方案2】:

    这是三个独立的问题,但无论如何:

    1.

    无论地图比例如何,GMSMarker 始终以相同的大小(以像素为单位)绘制。对于随地图缩放的图像,请查看GMSGroundOverlay

    2.

    您已将半径定义为 3000 米,因此它应始终为该尺寸(以米为单位),并随地图比例缩放。或者您是否希望它以像素为单位固定大小,以便在缩小时以米为单位变大?

    3.

    您需要将圈子存储为成员(就像使用标记一样),并在每次移动时更新其位置,而不是总是创建一个新圈子。

    【讨论】:

    • 虽然这个回复不是我的确切解决方案,但它确实为我指明了正确的方向。我没有玩弄GMSGroundOverlay,而是将我的图标大小更改为GMSMarker。对我的应用程序的进一步审查表明,半径正在缩放。我只需要在地图上设置缩放,以便能够正确适应mapView 中的半径。 3号是关键!我不敢相信这没有发生在我身上,但也许我太专注了。
    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2015-09-26
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多