Store locatore, how to use/input my postion?

孤人 提交于 2020-01-06 11:04:08

问题


I am creating a store locator. What is the best way for the user to inputs its location. I need some way to convert the user input into latitude and longitude.

How do I go from that the user types something in to converting it into latitude and longitude.

Thanks


回答1:


First, include the initialization code on page load:

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

Trigger a function using the onclick attribute or submission of a form.

Then in the function you trigger drop this in (note: this uses jQuery):

var address = $('#name_of_input_box').val();

Then geocode the address in the text box using the Google Geocoder API:

var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function (result, status) {
  cur_lng = result[0].geometry.location.lng();
  cur_lat = result[0].geometry.location.lat();
  var location = new google.maps.LatLng(cur_lat, cur_lng);
});

map.setCenter(location);



回答2:


You need to use the geocoder. See this links below

To use API V3 https://developers.google.com/maps/documentation/javascript/reference#Geocoder

To use WebService https://developers.google.com/maps/documentation/geocoding/index

If you need help about that, pleas ask again

Regards cadetill



来源:https://stackoverflow.com/questions/11663765/store-locatore-how-to-use-input-my-postion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!