centering a google map based on marker lat long values -


i'm plotting 20 markers on map, locations in city. when use getlatlng find city center it's little removed locations i'm plotting, means markers located in corner of map instead of in center.

is there way center map on location more central locations? i'm guessing need sort of "average" lat / long coordinate calculated 20 lat / long values have available.

is possible?

finding center should pretty easy. find smallest , largest latitude , longitude coordinates in group of 20.

the center [min_lat + (max_lat - min_lat)/2, min_long + (max_long - min_long)/2].

something like:

var points = array();  points.push(new glatlng( xxx, yyy ));  ...  min_lat = 90; max_lat = -90; min_long = 180;  max_long = -180;  (p in points) {     if (p[0] < min_lat) {         min_lat = p[0];     }     if (p[0] > max_lat) {          max_lat = p[0];     }     if (p[1] < min_long) {         min_long = p[1];     }     if (p[1] > max_long) {          max_long = p[1];     } }  center = new glatlng(min_lat + (max_lat - min_lat) / 2,                       min_long + (max_long - min_long) / 2]; map.setcenter(center, 10);  

this code have trouble if points cross longitude boundaries (±180°). it's assuming points located on flat surface (rather surface of sphere).

none of should matter if you're talking coordinates within several hundred miles of each other in us.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -