It doesn't need to be 100% correct, it can be the center of the bounding rectangle.
相关问题
- How do you change the color of the dotted line on
- How do you change the color of the dotted line on
- Union of many (more than two) polygons without hol
- draw polylines with different colors on v2 maps
- Add HTML class/ID to Google Maps Marker (API V3)
相关文章
- Avoiding “Sorry, we have no imagery here” with Goo
- Custom Marker performance iOS, crash with result “
- Google Map Gradient Polylines for Android?
- Angular 6 - @types/googlemaps/index.d.ts' is n
- How to make holes in a Polygon in shapely python,
- Google Maps - Get Polygon border of zones/neighbor
- Google Map Marker Clickable Area
- getSupportFragmentManager().findFragmentById retur
Matthew's answer is a good solution. However, when using the Google Maps API v3, you might want to pass each point of the polygon to a
LatLngBounds
object through theextend()
method, and then finally call thegetCenter()
method on theLatLngBounds
object. Consider the following example:Note that in the case of a concave polygon the center of the bounding rectangle might be completely outside the polygon. If your polygons might be concaved, I'd recommend using the center of the biggest inscribed circle as the "center" of the polygon. You can see a simple enough algorithm here (p. 4). If your task is to place a label on the polygon, this will also give the most aesthetically pleasing results (in which case I'd recommend using this method even if your polygons might not be concave).
Here is a custom function I wrote. Feel free to use it.
you can extend the Polygon class with your own version of the missing function, let's call it my_getBounds() :
and than use it in code like this :
... etc, it should be equivalent to the v2 behavior
Algorithm:
Run through all the points in the polygon. For all the points find;
x1
, the lowestx
coordinatey1
, the lowesty
coordinatex2
, the highestx
coordinatey2
, the highesty
coordinateYou now have the bounding rectangle, and can work out the center using: