google-maps-ios-utils icon indicating copy to clipboard operation
google-maps-ios-utils copied to clipboard

get feature clicked?

Open fukemy opened this issue 8 years ago • 5 comments

hello. I have a question: How to get GMUGeometry coordinates when click on map

fukemy avatar Sep 12 '18 08:09 fukemy

You want to use the GMSProjection object within the GMSMapView object. It should be able to translate lat/long to a CGPoint referencing the screen coords.

kwilk83 avatar Jan 23 '19 15:01 kwilk83

Once we obtain screens coordinates how will we determine which feature was clicked? This is still very unclear

prachiFam avatar May 25 '19 07:05 prachiFam

I was able to detect click using below code:

you can call this function when mapview is tapped ie. inside below method. func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)

    let path = Bundle.main.path(forResource: "dld_areas", ofType: "json")
    let url = URL(fileURLWithPath: path!)
  
    let data: Data = try! Data.init(contentsOf: url)
    let json = try JSON(data: data)
    //our json is having properties containing area name and area id, bacause the properties field inside the GMUFeature is retuning us nil, we have to pase it indivisually
    let jsonFeatures: [JSON] = json["features"].array ?? []
    
    geoJsonParser = GMUGeoJSONParser.init(data: data)
    geoJsonParser.parse()
    
    var index = 0
    
    for feature in geoJsonParser.features {
        
        
        var innerFeature = feature
        while(innerFeature.geometry.type != "GeometryCollection"){
            print("type is" + innerFeature.geometry.type)
            
            innerFeature = innerFeature.geometry as! GMUGeometryContainer
            
        }
        
        
        let collection = innerFeature.geometry as! GMUGeometryCollection
        
        let polygon:GMUPolygon = collection.geometries.first as! GMUPolygon
        var isAreaObtained = false
        
        for path in polygon.paths{
            
            //check if the coordinates lie within the polygon
            if (GMSGeometryContainsLocation(coordinates, path, true)) {
                
                isAreaObtained = true
                print(jsonFeatures[index]["properties"]["NAME_EN"])
                print(jsonFeatures[index]["properties"]["AREA_ID"])
                
                print("location inside polygon")
                break;
                
                
            }
        }
        if(isAreaObtained){
            break
        }
        
        index = index + 1
    }

prachiFam avatar May 25 '19 12:05 prachiFam

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

stale[bot] avatar Oct 03 '19 23:10 stale[bot]

Closing this. Please reopen if you believe it should be addressed. Thank you for your contribution.

stale[bot] avatar Nov 02 '19 23:11 stale[bot]