简体   繁体   English

将Google地图坐标传递到标记

[英]Pass google maps coordinates to marker

I am looking to pass a set of coordinates received from two textfields(displayLat and displayLong) and create a marker with the passed in coordinates as the marker location. 我希望传递从两个文本字段(displayLat和displayLong)接收的一组坐标,并创建一个以传递的坐标为标记位置的标记。 Code so far: 到目前为止的代码:

    <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>

So far I am passing in the coordinates, converting them into a google.maps.LatLng object but they are not getting passed to the placeMarker function so far as I can tell. 到目前为止,我正在传递坐标,将其转换为google.maps.LatLng对象,但据我所知,它们尚未传递给placeMarker函数。 Thanks in advance. 提前致谢。

function InputCoords() calls function placeMarker() which is not visible because it is local to initialize() function. 函数InputCoords()调用函数placeMarker() ,该函数不可见,因为它在initialize()函数中是本地的。 You have to place it out of it. 您必须将其放入其中。 And make variables map and markersArray global. 并使变量mapmarkersArray全局变量。

See example at jsbin . 请参阅jsbin上的示例

You can also add: 您还可以添加:

map.setCenter(location);

to placeMarker() function to see marker if location is off the current visible part of map. placeMarker()函数以查看位置是否在地图的当前可见部分之外的标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM