如何为GMSMarker创建边界

时间:2019-01-01 06:41:15

标签: ios swift google-maps geofencing

我有两个GMSMarker是一个正在移动,另一个没有移动,我想对geofencing创建一个更微笑的东西,如果移动的标记进入或离开了一个不移动的边界,我能够执行一些操作,

我通过Google找到了地理围栏API,但它仅适用于android,有没有办法迅速做到这一点?

可以使用GMSCircle吗?

2 个答案:

答案 0 :(得分:2)

请检查以下代码。这是通过设置纬度(每个点的长度)手动创建的区域。您可以通过传递纬度,创建区域长函数来设置区域并绘制圆形区域。然后利用CLLocationManager委托监视标记。

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代表

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")
}

答案 1 :(得分:0)

您尝试过CLRegion吗?

在半径不动的标记处创建一个区域,并调用其委托方法进入和退出运动标记。

,对于CLLocationManger,您可以开始监视区域。