开发者

Get latitude and longitude from a google map

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


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.

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() );
  }
});

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>

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();
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜