【发布时间】: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 的东西。谢谢!
编辑 我的代码确实有效,但半径无法缩放,并且图标出现在图标上方。当我放大地图时,我能够看到半径。存在三个问题:
- 图标不会根据地图的缩放级别进行缩放。
- 半径不会根据地图的缩放级别进行缩放。
- 如果标记位置更新,半径不会随之移动。相反,绘制了一个新的半径......所以地图上有两个半径。
【问题讨论】:
-
你能描述一下它是怎么出错的,和/或张贴截图吗?
-
我已经更新了我的帖子。显然我的代码确实有效,只是不完全符合我的预期。我需要解决 3 个问题。
标签: ios swift google-maps-sdk-ios