im new to google maps, i used html5 to get user's cordinates of latitude and longitude but i wanted to get the nearest place name of the user so that he may select that,(why type if we can click?) i googled and googled but cant do any thing. i have written these scripts today....
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your position:</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true">
//only a v3 api library
</script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
lat=position.coords.latitude;
lon=position.coords.longitude;
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('mapholder')
mapholder.style.height='250px';
mapholder.style.width='500px';
var myOptions={
center:latlon,zoom:18,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
};
var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
var marker=new google.maps.Marker({position:latlon,map:map,title:"Some where here"});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({latLng:latlon},function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
infoWindow.setContent(results[0].formatted_address);
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
}
}
});
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
</script>
</body>
</html>
i have done all these so far, now what i need to do is that, i want to get the place name for the location given by those two variables:lat,lon
so that i can find the place name, can any one help me out?
i have found this link which may be helpful but i cant figure it out,,, i dont need the placeinfo in the map, but i want to get place name in a variable
thanks in advanced.. In case question is not clear, please mention me, and ill try to be more clear,,,, i apologize for my language....
You already get all what you need, a geocoding-result. (results inside the geocode()-callback)
Those results(an array) consist of different items, one of them should have a member types(also an array) where the 2nd item is set to "locality" , this usually marks the item as a city. When you found this item, the long_name - member of the item will give you the city-name.
More info: https://developers.google.com/maps/documentation/javascript/geocoding?hl=en#GeocodingAddressTypes
A note related to the code:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true">
//only a v3 api library
</script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
you're loading the maps-API twice, because the first <script/>
will load the maps-API and the places-library. You better remove the 2nd <script/>
alert(results.formatted_address)//says undefined
and others these too
alert(results[0])//it says object
alert(results.street_address)//it says undefined
alert(results.locality)//it also says undefined
but nothing happened every time it alerted an objec - acidburn 2012-04-04 04:15
alert(JSON.stringify(results))
but i cant do any thing please hel - acidburn 2012-04-04 04:48
results[0].formatted_address
Dr.Molle 2012-04-04 07:54