pethel
pethel

Reputation: 5537

Store locatore, how to use/input my postion?

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

Upvotes: 0

Views: 1680

Answers (2)

Matt Kneiser
Matt Kneiser

Reputation: 2156

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

Upvotes: 2

cadetill
cadetill

Reputation: 1552

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

Upvotes: 1

Related Questions