Auto-center map with multiple markers in Google Maps API v3

后端 未结 8 999
梦如初夏
梦如初夏 2020-12-07 06:38

This is what I use to display a map with 3 pins/markers:



        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 07:33

    I think you have to calculate latitudine min and longitude min: Here is an Example with the function to use to center your point:

    //Example values of min & max latlng values
    var lat_min = 1.3049337;
    var lat_max = 1.3053515;
    var lng_min = 103.2103116;
    var lng_max = 103.8400188;
    
    map.setCenter(new google.maps.LatLng(
      ((lat_max + lat_min) / 2.0),
      ((lng_max + lng_min) / 2.0)
    ));
    map.fitBounds(new google.maps.LatLngBounds(
      //bottom left
      new google.maps.LatLng(lat_min, lng_min),
      //top right
      new google.maps.LatLng(lat_max, lng_max)
    ));
    

提交回复
热议问题