Call function initmap with parameters in gmaps api

荒凉一梦 提交于 2020-01-17 08:35:33

问题


I have this function, the default function from google maps api tutorial, I have just added 2 parameters latt and lngg.

function initMap(latt,lngg) {


    var uluru = {lat: latt , lng: lngg};
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 15,
      center: uluru
    });
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }

And I want to call it like they call it :

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAmjE35Pm1eDdyF6Vg3WC69opkqLIVt3GY&callback=initMap">
</script>

but using different parameters each time, because I will have 6000 maps, and I want to call the script for each function

I don't know if something like &callback=initMap(latt,lngg) is possible.

Thank you so much


回答1:


You can't add parameters to a function that is used as a callback. You can add variables in the global scope or in your HTML that can be used in the callback function.

For example:

<div id="mapcoords" data-lat="40" data-lng="-117" data-zoom="5"></div>

function initMap() {
  var uluru = {
    lat: parseFloat(document.getElementById('mapcoords').getAttribute("data-lat")), 
    lng: parseFloat(document.getElementById('mapcoords').getAttribute("data-lng"))
  };
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: parseInt(document.getElementById('mapcoords').getAttribute("data-zoom")),
    center: uluru
  });
  var marker = new google.maps.Marker({
    position: uluru,
    map: map
  });
}

proof of concept fiddle

code snippet:

function initMap() {
  var uluru = {
    lat: parseFloat(document.getElementById('mapcoords').getAttribute("data-lat")),
    lng: parseFloat(document.getElementById('mapcoords').getAttribute("data-lng"))
  };
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: parseInt(document.getElementById('mapcoords').getAttribute("data-zoom")),
    center: uluru
  });
  var marker = new google.maps.Marker({
    position: uluru,
    map: map
  });
}
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<div id="map"></div>
<div id="mapcoords" data-lat="40" data-lng="-117" data-zoom="5"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>



回答2:


You could have a separate script which sets the variables, and then have the initMap() call these variables.

var latt;
var lngg;

function setCoords(latt,lngg){
  this.latt = latt;
  this.lngg = lngg;
}

function initMap() {

    var uluru = {lat: latt , lng: lngg};
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 15,
      center: uluru
    });
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }
<script type ='text/javascript'>setCoords(33.225488,-111.5955)</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAmjE35Pm1eDdyF6Vg3WC69opkqLIVt3GY&callback=initMap">
</script>


来源:https://stackoverflow.com/questions/47104164/call-function-initmap-with-parameters-in-gmaps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!