从谷歌地图获取纬度和经度

从谷歌地图获取纬度和经度

问题描述:

我希望我的用户能够在地图上选择一个地方,并返回可以上传到数据库的纬度和经度。任何帮助将不胜感激

I would like my user to be able to select a place on a map and return me the lat and lon which can be uploaded to a database. Any help would be appreciated

因为您正在处理Google Maps API(基于您问题的标记) JavaScript中的内置API功能的优势可能会有所帮助。您可以前往这个地方开始学习JavaScript,然后阅读 API,最后将能够使用JavaScript的值已经由JavaScript自动设置,并且用户在地图上点击的纬度和经度。

Because you are dealing with Google Maps API (based on your question's tag), I guess taking taking advantage of the in-built API capabilities in JavaScript might help. You can go over to this place to get started learning JavaScript, you will,then read the API here, and finally will be able to post a form using JavaScript whose values have been auto-set by the JavaScript with the latitude and longitude of the point the user clicked on the map.

以下是您可能需要做的一些操作,但您可能需要学习一些JavaScript来扩展或有效使用它:

Here is a bit of what you might have to do, but you might need to learn some JavaScript to extend it or use it effectively:

var map = new GMap2(document.getElementById("map_canvas"));

map.setCenter(new GLatLng(37.4419, -122.1419), 13);

GEvent.addListener(map,"click", function(overlay, latlng) {     
  if (latlng) { 
    alert("Latitude : " + latlng.lat() + " , Longitude: " + latlng.lng() );
  }
});

因此,假设您使用表单创建一个简单的html页面来保存纬度和经度,如下所示:

So, assuming you create a simple html page with a form to hold the latitude and longitude like this:

<form action="some_server_side_script_or_path" method="POST" id="map_form">
 <!-- display the map -->
 <div id="map_canvas"></div>
 <label for="mlatitude">Male</label>
  <input type="text" name="mlatitude" id="mlatitude" readonly="readonly"/>
  <br />
<label for="mlongitude">Male</label>
  <input type="text" name="mlongitude" id="mlongitude" readonly="readonly"/>
  <br />
<input type="submit" value="Submit Location" />
</form>

然后,通过修改我们的clik处理程序,我们可以自动设置纬度和经度然后让用户只需单击提交即可将值发布到服务器。

Then, by modfying our clik-handler like this, we can auto-set the latitude and longitude on the form, and then have the user simply click submit to post the values to the server.

GEvent.addListener(map,"click", function(overlay, latlng) {     
  if (latlng) { 
    var lat = document.getElementById("mlatitude");
    var lng = document.getElementById("mlongitude");
    lat.value = latlng.lat();
    lng.value = latlng.lng();
    //at this point, you can even use Javascript to auto-submit the form using
    //document.forms["map_form"].submit();
  }
});