如何使用 Google Maps SDK (Swift/iOS) 在地图标记周围绘制半径?
How Can I Draw A Radius Around A Map Marker Using Google Maps SDK (Swift/iOS)?
用户可以通过两种方式在应用内的地图上放置标记 - 通过地图上方的 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 地图 API v3 的内容。谢谢!
编辑
我的代码确实有效,但半径没有缩放,图标出现在图标上方。当我放大地图时,我能够看到半径。存在三个问题:
- 图标不会根据地图的缩放级别进行缩放。
- 半径不会根据地图的缩放级别进行缩放。
- 如果标记位置更新,半径不会随之移动。相反,绘制了一个新的半径……所以地图上有两个半径。
这是三个独立的问题,但无论如何:
1.
无论地图比例如何,GMSMarker
始终以相同大小(以像素为单位)绘制。对于随地图缩放的图像,请查看 GMSGroundOverlay
。
2.
您已将半径定义为 3000 米,因此它应该始终以米为单位的大小,因此会随着地图的比例尺进行缩放。或者您是否希望它以像素为单位固定大小,以便在缩小时以米为单位变大?
3.
您需要将圆存储为成员(就像您对标记所做的那样),并在每次移动时更新其位置,而不是总是创建一个新圆。
这里是 swift 代码,用于在以英里为单位的指定半径上绘制圆
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;
用户可以通过两种方式在应用内的地图上放置标记 - 通过地图上方的 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 地图 API v3 的内容。谢谢!
编辑 我的代码确实有效,但半径没有缩放,图标出现在图标上方。当我放大地图时,我能够看到半径。存在三个问题:
- 图标不会根据地图的缩放级别进行缩放。
- 半径不会根据地图的缩放级别进行缩放。
- 如果标记位置更新,半径不会随之移动。相反,绘制了一个新的半径……所以地图上有两个半径。
这是三个独立的问题,但无论如何:
1.
无论地图比例如何,GMSMarker
始终以相同大小(以像素为单位)绘制。对于随地图缩放的图像,请查看 GMSGroundOverlay
。
2.
您已将半径定义为 3000 米,因此它应该始终以米为单位的大小,因此会随着地图的比例尺进行缩放。或者您是否希望它以像素为单位固定大小,以便在缩小时以米为单位变大?
3.
您需要将圆存储为成员(就像您对标记所做的那样),并在每次移动时更新其位置,而不是总是创建一个新圆。
这里是 swift 代码,用于在以英里为单位的指定半径上绘制圆
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;