Как создать Bounder для GMSMarker - PullRequest
       65

Как создать Bounder для GMSMarker

0 голосов
/ 01 января 2019

У меня есть два GMSMarker, где один из них движется, а другой нет, и я хочу создать что-то более смелое для geofencing, где, если движущийся маркер вошел или оставил границу неподвижного, я смогу сделатьнекоторые действия,

Я нашел API геозонирования от Google, но это только для Android, есть ли способ сделать это в Swift?

можно ли использовать GMSCircle для этого?

Ответы [ 2 ]

0 голосов
/ 02 января 2019

Пожалуйста, проверьте ниже код.Вот создали область вручную, установив широту, длинную для каждой точки.Вы можете установить свой регион, передав широту и длину в функции создания области и нарисовав круговую область.Затем используйте делегат 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")
}
0 голосов
/ 01 января 2019

Вы пробовали CLRegion?

создать одну область на неподвижном маркере с радиусом и вызвать его метод делегата для входа и выхода из движущегося маркера.

и для CLLocationManger вы можете запустить мониторинг дляобл.

...