Reputation: 773
I have two GMSMarker
were one of the is moving and the other is not and I want to create something smilier to geofencing
where if the moving marker entered or left the bounder of the unmoving one I will able to do some actions,
I found an geofencing APIs by google but it is for android only, is there a way to do this in swift?
can the GMSCircle
be used to do so?
Upvotes: 1
Views: 277
Reputation: 1754
Please check below code. Here is have created the area manually by setting the lat, long for each point. You can set your region by passing the lat,long in create area function and draw a circular region. Then make use of CLLocationManager delegate to monitor the marker.
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 Delegates
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")
}
Upvotes: 2
Reputation: 69
Have you tried CLRegion?
create one region at not moving marker with radius and call its delegate method for entering and exiting the moving marker.
and for CLLocationManger you can start monitoring for region.
Upvotes: 0