Jar*_*red 7 ios google-maps-sdk-ios swift
用户可以通过两种方式将标记放置在应用程序中的地图上 - 通过地图上方的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
}
Run Code Online (Sandbox Code Playgroud)
不可否认,这是我的第一个iOS/Swift应用程序.我想如果我将坐标从didLongPressAtCoordinate传递给drawCircle()函数,但显然我做错了.我一整天都在寻求帮助,但只提供Android和Google Maps API v3的内容.谢谢!
编辑 我的代码确实有效,但半径不能缩放,图标显示在图标上方.当我放大地图时,我能够看到半径.有三个问题:
这是在英里指定半径上绘制圆圈的快速代码
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;
Run Code Online (Sandbox Code Playgroud)
这是三个独立的问题,但无论如何:
1.
GMSMarker无论地图比例如何,始终以相同的尺寸(以像素为单位)绘制。对于随地图缩放的图像,请查看GMSGroundOverlay。
2.
您已将半径定义为 3000 米,因此它应始终以米为单位,并根据地图的比例进行缩放。或者您希望它以像素为单位的固定大小,因此在缩小时以米为单位变大?
3.
您需要将圆圈存储为成员(就像使用标记一样),并在每次移动时更新其位置,而不是总是创建一个新圆圈。
| 归档时间: |
|
| 查看次数: |
7353 次 |
| 最近记录: |