ios - 如何为 GMSMarker 创建边界

标签 ios swift google-maps geofencing

我有两个 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/

相关文章:

ios - 如何在 Swift 中打印出核心数据保存错误

iphone - 如何在 IOS 中绘制 3d 条形图?

ios - 如何为 UITableView 下面具有可变高度的项目设置约束

ios - 无法在 iOS 应用程序中打开 Google map

Android:给定一个点设置谷歌地图缩放和位置的算法

api - 谷歌地理编码组件两个国家

ios - fatal error : unexpectedly found nil while unwrapping an Optional value swift When Selecting tableView Cells

ios - 为什么 UIView 的 intrinsicContentSize 总是返回 (-1.0, -1.0)?

swift - 通过类实例访问结构

ios - 以编程方式为 iPhone 和 iPad 布局 UI 的最佳实践