2

I'm working on a project where I make a call to my API to get back GeoJSON for a 3 mile radius from a given address. It returns back towns for me in a feature collection by using PostGIS/Tiger. The data comes back correct and works.

I then load this data into Google Maps like so:

 map.data.addGeoJson(data);

Is there a way to now take the geojson that is loaded, and center in on that point?

I have a feature collection of polygons for an address in a 5 mile radius. I was hoping I could just get the center without having to update my SQL query and return it from the API.

3
  • do you mean loadGeoJson? developers.google.com/maps/documentation/javascript/… – Stephen Lead Jul 19 '17 at 4:56
  • What about using the point your using for your 3 mile radius? Isn't that waht you really want to center the view on? Then just use map.setCenter({lat:LAT_VALUE, lng:LONG_VALUE}); – Bill Chappell Jul 19 '17 at 12:29
  • @BillChappell I'm taking an address the user types in, and then geocoding it to an area of GeoJSON. The response I get back is GeoJSON so I have to either parse that, or have my response include two things now (GeoJSON, and a long/lat to center in on which would have to be done on the database side). I don't mind doing it, but was hoping that there was a client-side way. – Mike-751031 Jul 19 '17 at 21:40
0

If you may use js library, turfjs can do that. Take a look at http://turfjs.org/docs/#center . It is stated that "Takes a Featureor FeatureCollectionand returns the absolute center point of all features." You may zoom to returned point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.