开发者

lat and long with submit button

开发者 https://www.devze.com 2023-04-12 23:26 出处:网络
I\'m and intern at an company that has asked me to put some markers on a google maps. I did this with the lat and long since geocoding the addresses and than making markers out of that would take to l

I'm and intern at an company that has asked me to put some markers on a google maps. I did this with the lat and long since geocoding the addresses and than making markers out of that would take to long to load.

However, they now also asked if I could make an easy application to get the lat and long from addresses. So a simple geocoding app:

One input field and a submit button, when clicked on the button the lat and long will show up below or above or next to it.

Which function can i use to get this?

I've been l开发者_JAVA百科ooking on the internet for a few days now, but I can't find anything that also makes it so that i can show the lat and long that was found.

I'm not very good at programming.


function get_tatitude_longitude(address){
var mygc = new google.maps.Geocoder();
mygc.geocode({'address' : address}, function(results, status){
    alert( "latitude : " + results[0].geometry.location.lat() );
    alert( "longitude : " + results[0].geometry.location.lng() );
 lat_c = results[0].geometry.location.lat();
long_c = results[0].geometry.location.lng();
mapa();
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('long').value = results[0].geometry.location.lng();
document.getElementById('latlongclicked').value = marker.getPosition();

});

}


Im confused, are you trying to find the lat and lng of an address?

if so use google maps api http://code.google.com/apis/maps/documentation/geocoding/

here is an example http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

and that will spit back the lat and lng in a json format.

If you want to reverse geocode something http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

To make an app that does it with a submit button, use jquery/ajax and pull the result from googles service.


Perhaps given you experience you should try using gmap3 for jQuery. It uses the Google Maps API v3, but abstracts the API making it a lot easier to use. They have an example you should study. It takes an address and returns a possible location.

Below I have modified the example which will alert the location – hopefully to giving you a head start:

$("#test").gmap3();

$('#address').autocomplete({
  //This bit uses the geocoder to fetch address values
  source: function(request, response) {
    $("#test").gmap3({
      action:'getAddress',
      address: request.term,
      callback:function(results){
        if (!results) return;
        response($.map(results, function(item) {

          alert("The location is", item.geometry.location.lat(), item.geometry.location.lon());

          return {
            label:  item.formatted_address,
            value: item.formatted_address,
            latLng: item.geometry.location
          }
        }));
      }
    });
  },
  //This bit is executed upon selection of an address
  select: function(event, ui) {
    $("#test").gmap3(
      {action:'clear', name:'marker'},
      {action:'addMarker',
        latLng:ui.item.latLng,
        map:{center:true}
      }
    );
  }
});
0

精彩评论

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

关注公众号