DiegoP.
DiegoP.

Reputation: 45737

HTML5 jQuery GeoLocation with Google Map

Hello I am using the following GeoLocation function to get the location of a visitor

function success(position) {
  var s = document.querySelector('#location');

  if (s.className == 'success') {
    return;
  }

  s.value = "found you!";
  s.className = 'success';

  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapcanvas';
  mapcanvas.style.height = '200px';
  mapcanvas.style.width = '380px';

  document.querySelector('article').appendChild(mapcanvas);

  var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeControl: false,
    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);

  var marker = new google.maps.Marker({
      position: latlng, 
      map: map, 
      title:"You are here!"
  });
}

function error(msg) {
  var s = document.querySelector('#status');
  s.innerHTML = typeof msg == 'string' ? msg : "failed";
  s.className = 'fail';

  // console.log(arguments);
}

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  error('not supported');
}

As you see it write in the field the value "found you!" when the process is successful.

Now what I need instead of writing "found you!" I need to write the location of the visitor.

So if the member is in Rome, it will write "Rome, Italy"

How do I achieve it?

Thanks for your help!!!

Upvotes: 1

Views: 2280

Answers (1)

Libby
Libby

Reputation: 866

You want geocoding (or reverse geocoding). I wrote a short overview here on my support forum. See the links near the end of the post.

A more involved example: my tutorial using google maps, jquery, markers and geocoding, includes source for getting full address from lat/lng coords.

Upvotes: 1

Related Questions