Javascript 将谷歌地图坐标传递给标记

Javascript 将谷歌地图坐标传递给标记,javascript,forms,google-maps,google-maps-api-3,marker,Javascript,Forms,Google Maps,Google Maps Api 3,Marker,我希望传递从两个文本字段(displayLat和displayLong)接收到的一组坐标,并使用传入的坐标作为标记位置创建一个标记。 迄今为止的代码: <html> <head> <script> function initialize() { var markersArray = []; //array to hold the map markers function clearOv

我希望传递从两个文本字段(displayLat和displayLong)接收到的一组坐标,并使用传入的坐标作为标记位置创建一个标记。 迄今为止的代码:

    <html>
    <head>
    <script>
    function initialize()
    {
    var markersArray = [];          //array to hold the map markers

    function clearOverlays() {      //function to clear the markers from the arrays, deleting them from the map
      for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
      }
      markersArray.length = 0;
    }
    var mapProp = {     //the actual map in the page
      center:new google.maps.LatLng(51.8978719,-8.471087399999988),     //center of map
      zoom:12,          //zoom level
      mapTypeId:google.maps.MapTypeId.ROADMAP       //kind of map
      };
    var map=new google.maps.Map(document.getElementById("googleMap")        //element ID
      ,mapProp);

    google.maps.event.addListener(map, 'rightclick', function(event) {      //what happens when the map is right clicked
        clearOverlays();            //removes current markers form the map
      });

    function placeMarker(location) {        //place marker function, adds marker to passed in location
var marker = new google.maps.Marker({
position: location,
map: map,
      })
      markersArray.push(marker);        //adds new marker to the markers array
      google.maps.event.addListener(marker,"click",function(){});           
      ;

    google.maps.event.addListener(marker, 'click', function() {     //listener so when a marker is clicked an infowindow pops up
infowindow.open(map,marker);
      });
    }
    }

    function InputCoords()
    {
    var Inputlat = document.getElementById('displayLat').value;
    var Inputlng = document.getElementById('displayLong').value;
    var newLatLng = new google.maps.LatLng(Inputlat, Inputlng); 

    document.getElementById("button1").innerHTML=newLatLng;

    placeMarker(newLatLng); 

    document.getElementById("button1").innerHTML="Past var dec";

    }

    google.maps.event.addDomListener(window, 'load', initialize);


    </script>
    </head>

    <body>
    <div id="googleMap" style="width:500px;height:380px;"></div>
    Lat 1:<input type="text" size="20" maxlength="50" name="displayLat" id ="displayLat" value=""><br />
    Long 1:<input type="text" size="20" maxlength="50" name="displayLong" id="displayLong" value=""><br />

    <button onclick="InputCoords()">Input Coordinates</button>
    <p id="button1"></p>


    </body>
    </html>

函数初始化()
{
var markersArray=[];//保存地图标记的数组
函数clearOverlays(){//用于从数组中清除标记,并将其从映射中删除
对于(var i=0;i
长1:
输入坐标

到目前为止,我正在传递坐标,将它们转换为google.maps.LatLng对象,但据我所知,它们没有传递到placeMarker函数。 提前感谢。

函数
InputCoords()
调用函数
placeMarker()
,该函数不可见,因为它是
initialize()
函数的本地函数。你必须把它放在外面。并使变量
map
markersArray
全局

您还可以添加:

map.setCenter(location);

使用
placeMarker()
函数查看标记位置是否在地图的当前可见部分。

完美的解决方案,我曾尝试将InputCoords()和placeMarker()放在initialize()之外,但没有注意到需要全局变量,非常感谢。您必须在控制台日志打开的情况下进行开发/测试。
map
的消息不太清楚,但
markersArray
的消息是:
未捕获引用错误:未定义markersArray