Google maps api 3 通过谷歌地图API V3检索可拖动pin的纬度和经度

Google maps api 3 通过谷歌地图API V3检索可拖动pin的纬度和经度,google-maps-api-3,draggable,latitude-longitude,Google Maps Api 3,Draggable,Latitude Longitude,我会解释的。我设法在地图上有一个可拖动的别针。我想检索这个点的坐标,并将它们放入两个字段:纬度和经度。这些坐标稍后将通过PHP发送到SQL表。 这里是我打算做的,但不是几个引脚,它只是一个,它的拖拽。问题是:我甚至不能显示初始点的坐标。当然,当用户移动pin时,我希望字段中的坐标也发生变化。 我希望我说清楚了。我做错了什么?我应该使用这项服务吗 下面是JS: <script type="text/javascript"> var map; function initializ

我会解释的。我设法在地图上有一个可拖动的别针。我想检索这个点的坐标,并将它们放入两个字段:纬度和经度。这些坐标稍后将通过PHP发送到SQL表。 这里是我打算做的,但不是几个引脚,它只是一个,它的拖拽。问题是:我甚至不能显示初始点的坐标。当然,当用户移动pin时,我希望字段中的坐标也发生变化。 我希望我说清楚了。我做错了什么?我应该使用这项服务吗

下面是JS:

<script type="text/javascript">
  var map;
  function initialize() {
  var myLatlng = new google.maps.LatLng(40.713956,-74.006653);

  var myOptions = {
     zoom: 8,
     center: myLatlng,
     mapTypeId: google.maps.MapTypeId.ROADMAP
     }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

  var marker = new google.maps.Marker({
  draggable: true,
  position: myLatlng, 
  map: map,
  title: "Your location"
  });

  google.maps.event.addListener(marker,'click',function(overlay,point){
     document.getElementById("latbox").value = lat();
     document.getElementById("lngbox").value = lng();
     });

}
</script> 

var映射;
函数初始化(){
var mylatng=newgoogle.maps.LatLng(40.713956,-74.006653);
变量myOptions={
缩放:8,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
var marker=new google.maps.marker({
真的,
职位:myLatlng,
地图:地图,
标题:“你的位置”
});
google.maps.event.addListener(标记,'click',函数(覆盖,点){
document.getElementById(“latbox”).value=lat();
document.getElementById(“lngbox”).value=lng();
});
}
以及HTML:

<html>
<body onload="initialize()">

  <div id="map_canvas" style="width:50%; height:50%"></div>

  <div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
  </div>

</body>
</html>

纬度:

经度:

这两项工作中的任何一项

google.maps.event.addListener(marker, 'click', function (event) {
    document.getElementById("latbox").value = event.latLng.lat();
    document.getElementById("lngbox").value = event.latLng.lng();
});

google.maps.event.addListener(marker, 'click', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
});

您也可以考虑使用DeReDead事件也

google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
});

实际工作的代码如下所示:

google.maps.event.addListener(marker, 'drag', function(event){
       document.getElementById("latbox").value = event.latLng.lat();
       document.getElementById("lngbox").value = event.latLng.lng();
});

如果在销落下后地图可以重新居中会更好。我想可以用
map.setCenter()
来完成,但我不确定应该把它放在哪里。我试着把它放在这段代码之前和之后,但它不起作用。

谷歌地图V3示例。下面是一个工作示例,用户删除单个pin,用新pin替换删除的pin,自定义pin图像,在DIV的表单字段中填充lat/long值的pin

<html>
<body onLoad="initialize()">

  <div id="map_canvas" style="width:50%; height:50%"></div>

  <div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
  </div>

</body>
</html>

<cfoutput>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=#YOUR-GOOGLE-API-KEY#&sensor=false"></script>
</cfoutput>

<script type="text/javascript">
//<![CDATA[

    // global "map" variable
    var map = null;
    var marker = null;

    // popup window for pin, if in use
    var infowindow = new google.maps.InfoWindow({ 
        size: new google.maps.Size(150,50)
        });

    // A function to create the marker and set up the event window function 
    function createMarker(latlng, name, html) {

    var contentString = html;

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        zIndex: Math.round(latlng.lat()*-100000)<<5
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        });

    google.maps.event.trigger(marker, 'click');    
    return marker;

}

function initialize() {

    // the location of the initial pin
    var myLatlng = new google.maps.LatLng(33.926315,-118.312805);

    // create the map
    var myOptions = {
        zoom: 19,
        center: myLatlng,
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // establish the initial marker/pin
    var image = '/images/googlepins/pin2.png';  
    marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      icon: image,
      title:"Property Location"
    });

    // establish the initial div form fields
    formlat = document.getElementById("latbox").value = myLatlng.lat();
    formlng = document.getElementById("lngbox").value = myLatlng.lng();

    // close popup window
    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
        });

    // removing old markers/pins
    google.maps.event.addListener(map, 'click', function(event) {
        //call function to create marker
         if (marker) {
            marker.setMap(null);
            marker = null;
         }

        // Information for popup window if you so chose to have one
        /*
         marker = createMarker(event.latLng, "name", "<b>Location</b><br>"+event.latLng);
        */

        var image = '/images/googlepins/pin2.png';
        var myLatLng = event.latLng ;
        /*  
        var marker = new google.maps.Marker({
            by removing the 'var' subsquent pin placement removes the old pin icon
        */
        marker = new google.maps.Marker({   
            position: myLatLng,
            map: map,
            icon: image,
            title:"Property Location"
        });

        // populate the form fields with lat & lng 
        formlat = document.getElementById("latbox").value = event.latLng.lat();
        formlng = document.getElementById("lngbox").value = event.latLng.lng();

    });

}
//]]>

</script> 

纬度:

经度:

//
查看谷歌地图API参考中的官方代码示例:

对我来说效果很好。。谢谢..

检查此项

在下面的代码中,用所需的替换dragend。在您的情况下,“单击”

google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("defaultLatitude").value = event.latLng.lat();
    document.getElementById("defaultLongitude").value = event.latLng.lng();
});

试试这个:)


萨奈克庄园酒店
var-map=null;
var-geocoder=null;
函数初始化(){
if(GBrowserIsCompatible()){
map=newgmap2(document.getElementById(“map_canvas”);
赛特中心地图(新格拉特林(20.236046,76.988255),1);
map.setUIToDefault();
geocoder=新的GClientGeocoder();
}
}
函数showAddress(地址){
if(地理编码器){
geocoder.getLatLng(
地址:,
功能(点){
如果(!点){
警报(地址+“未找到”);
}否则{
地图设定中心(点15);
var marker=新的GMarker(点,{draggable:true});
添加覆盖图(标记);
addListener(标记“dragend”,函数(){
marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
});
addListener(标记“单击”,函数(){
marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
});
触发器(标记“单击”);
}
}
);
}
}


嘿,谢谢你,事实上我昨天用你提供的代码自己解决了这个问题。所以我很高兴我能得到同样的结果。昨天我本来打算回答我自己的问题,但斯塔克不让我回答。显然,我不能在8小时前回答我自己的问题(新用户)。我就是这么被告知的。无论如何,谢谢你的回答。我怎样才能在一个表格中同时显示这两个坐标?比如(-10.00000,10.00000)?Clean fiddle能够完全复制我想要的。这个链接不再活跃。
google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("defaultLatitude").value = event.latLng.lat();
    document.getElementById("defaultLongitude").value = event.latLng.lng();
});
    var zoomLevel = map.getZoom();
    var pos = (event.latLng).toString();

    $('#position').val(zoomLevel+','+pos); //set value to some input
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
develop by manoj sarnaik
 -->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Manoj Sarnaik</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxSPW5CJgpdgO_s4yyMovOaVh_KvvhSfpvagV18eOyDWu7VytS6Bi1CWxw"
      type="text/javascript"></script>
    <script type="text/javascript">

    var map = null;
    var geocoder = null;

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(20.236046, 76.988255), 1);
        map.setUIToDefault();
        geocoder = new GClientGeocoder();
      }
    }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point, {draggable: true});
              map.addOverlay(marker);
              GEvent.addListener(marker, "dragend", function() {
                marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
              });
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
              });
          GEvent.trigger(marker, "click");
            }
          }
        );
      }
    }
    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()">
    <form action="#" onsubmit="showAddress(this.address.value); return false">

      <p>
        <input type="text" style="width:350px" name="address" value="Malegaon,washim" />
        <input type="submit" value="Go!" />
      </p>
      <div id="map_canvas" style="width: 600px; height: 400px"></div>
    </form>

  </body>
</html>