我有两个 GMSMarker
,一个在移动,另一个在移动不动的边界我将能够做一些 Action ,
我通过谷歌找到了一个地理围栏 API,但它仅适用于 Android,有没有办法快速做到这一点?
GMSCircle
可以用来这样做吗?
最佳答案
请检查以下代码。这是通过为每个点设置纬度和经度来手动创建区域。您可以通过在 create area 函数中传递 lat,long 来设置您的区域并绘制一个圆形区域。然后使用 CLLocationManager 委托(delegate)来监视标记。
var selectedPlace: GMSPlace?
let rect = GMSMutablePath()
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
showPin(myPlace: place)
dismiss(animated: true, completion: nil)
}
func showPin(myPlace: GMSPlace){
mapView.clear()
createArea()
selectedPlace = myPlace
print((selectedPlace?.coordinate)!)
if(GMSGeometryContainsLocation((selectedPlace?.coordinate)!, rect, true))
{
print("YES: you are in this polygon.")
}else{
print("You do not appear to be in this polygon.")
}
func createArea(){
// Create a rectangular path
rect.add(CLLocationCoordinate2D(latitude: 18.882534, longitude: 72.801590))//Fort
rect.add(CLLocationCoordinate2D(latitude: 19.492122, longitude: 72.737045))//Virar
rect.add(CLLocationCoordinate2D(latitude: 19.476586, longitude: 73.096848))//Virar Right
rect.add(CLLocationCoordinate2D(latitude: 19.271893, longitude: 73.127060))//Kalyan
rect.add(CLLocationCoordinate2D(latitude: 19.186313, longitude: 73.243790))//Badlapur
//rect.add(CLLocationCoordinate2D(latitude: 18.811052, longitude: 73.547287))//Karjat
rect.add(CLLocationCoordinate2D(latitude: 18.966973, longitude: 73.107834))//Panvel
rect.add(CLLocationCoordinate2D(latitude: 19.021510, longitude: 72.975998))//Vashi
rect.add(CLLocationCoordinate2D(latitude: 18.882534, longitude: 72.801590))//Mumbai
// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView
}
//CLLocationManager委托(delegate)
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion){
print("Start Monitoring Region")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion){
print("Determine Region State")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion){
print("Entered Monitoring Region")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion){
print("Exited Monitoring Region")
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error){
print("Failed Monitoring Region")
}
关于ios - 如何为 GMSMarker 创建边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993519/