Find center of multiple locations in Google Maps

前端 未结 4 735
面向向阳花
面向向阳花 2020-11-27 12:14

I have just copied the code on this question and applied my latitude and longitudes. However, the latitudes and longitudes will be dynamic, and the center of th

4条回答
  •  無奈伤痛
    2020-11-27 12:48

    First you can create a LatLngBounds object by including all the dynamically generated locations. Use the extend method to include the points. Then you can get the center of the bound using the getCenter method.

    UPDATE:

    Code:

    var bound = new google.maps.LatLngBounds();
    
    for (i = 0; i < locations.length; i++) {
      bound.extend( new google.maps.LatLng(locations[i][2], locations[i][3]) );
    
      // OTHER CODE
    }
    
    console.log( bound.getCenter() );
    

    Illustration:

    enter image description here

提交回复
热议问题