I've been looking through version 3 of the google maps api, and I have put together a small script to geocode an address. The problem is that I want to see If i can extract the lat lng without having to 'split()' the result.
function getLatLng() {
var geocoder = new google.maps.Geocoder();
var query = "12 henry street, dublin 1, dublin, ireland";
var address = query;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
alert('Geocode succesful ' + latLng);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
I want to get the lat and the lng from the var latLng.
Can this be done without calling latLng.split(',');?
Thanks so much for your help
Never use internal variables exposed by the API. They can and do change from release to release. Always use documented methods.
results[0].geometry.location is a LatLng object, so use the relevant methods:
var lat=results[0].geometry.location.lat();
var lng=results[0].geometry.location.lng();
You can use this:
var lat= results[0].geometry.location.lat();
var lng= results[0].geometry.location.lng();
alert(lat);
alert(lng);
Related
I am using google maps api and it works for all but one specific address. It's "99p Stores 19-20 Market Place Wisbech PE13 1DZ". When using google maps I can find it, but using js api it says 'zero results'.
My code:
function mapsSetMark(map, address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setZoom(13);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
"99p Stores 19-20 Market Place Wisbech PE13 1DZ" is a place not a postal address. The Geocoder is specifically for postal addresses
It can find "19-20 Market Place Wisbech PE13 1DZ"
To find "99p Stores 19-20 Market Place Wisbech PE13 1DZ" use the Places API Library
I'm trying to use Google Maps Autocomplete API but there is some discrepancies between it and Geocoder API, for example:
On the autocomplete API, I'm trying to search the address "Rua Tiradentes, 1020, Ibirubá", when I select on the list, the address component "street_number" doesn't come, it's an issue with this and other addresses.
Let's try with geocoder API:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'Rua Tiradentes, 1020, Ibirubá' }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
} else {
alert("Geocoder failed due to: " + status);
}
});
Now, the street_number is inside the address_components, is there any workaround or Google Autocomplete isn't reliable?
Here is the places code:
var b = new google.maps.places.Autocomplete(myInput);
google.maps.event.addListener(b, 'place_changed', function () {
console.log(b.getPlace());
})
The possible workaround is the following. You can try to geocode the place to get all address components. Recently, Google added possibility to geocode addresses using the placeId as a request parameter.
You can find more detailed information in the documentation:
https://developers.google.com/maps/documentation/javascript/geocoding
https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderRequest
So, you can get the place like var place = autocomplete.getPlace(); you can obtain the place ID like place.place_id and execute geocoding request like this one:
geocoder.geocode( { 'placeId': place.place_id}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
I have used the sample code from the following URL.
http://earth-api-samples.googlecode.com/svn/trunk/examples/geocoder.html
I would like to show the location after the page loads. I am new to google earth.
Right now, I have removed the location form and called the buttonClick function under the initCB method. It did not works.
Please help.
You have either to pass the city to the geocoder in the code or to get the latitude/longitude before and insert it in the code.
-1. First option
The example use Google Maps v.2 API, while now you have available v.3 here: https://developers.google.com/maps/documentation/javascript/geocoding
var address = "Los Angeles";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
point = results[0].geometry.location;
alert(point);//this is to see the lat long values
var lookAt = ge.createLookAt('');
lookAt.set(point.y, point.x, 10, ge.ALTITUDE_RELATIVE_TO_GROUND,
0, 60, 20000);
ge.getView().setAbstractView(lookAt);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
-2. Second option
var lookAt = ge.createLookAt('');
lookAt.set(50.5, -79.5, 10, ge.ALTITUDE_RELATIVE_TO_GROUND,
0, 60, 20000);
ge.getView().setAbstractView(lookAt);
where 50.5 should be the latitude and -79.5 should be the longitude.
Hope it helps!
Cheers,
Mihai
What is making the following code get the country and not the city, and how can I change it to get the city instead of country?
function get_visitor_country() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var country = results[results.length-1].formatted_address;
$("#location-detection .location-name").html(country);
$("#geolocation-worked").css("display", "block");
$("#no-geolocation").css("display", "none");
$geolocation_fix.hide().css({ height : 0 });
init_geolocation_switch();
}
}
});
});
}
}
The script is also loading http://maps.google.com/maps/api/js?sensor=false at the end of the file, if that might be affecting it.
Your script currently is not getting anything special(like a city or a country) , it gets something out of the results.
To get the city search inside the results for an entry with types set to ["locality", "political"]
When you found it you got the city.
Creation of an object labeled with addressTypes for convenient access:
var components={};
jQuery.each(results,function(i,v){
components[jQuery.camelCase(v.types.join('-'))]=v;
})
//test it
alert((components.localityPolitical)
? components.localityPolitical.formatted_address
: 'n/a');
See: Address Component Types
I want to be able to plot several companies on a google map and understand I need to geocode these.
I also have the code below that plot's multiple markers on a map.
How can I Geocode several company addresses (using the following address as the first example) and incorporate it into the current code I have?
I really need someone's help as I can't make sense of the Google documentation as well as incorporating it with what I already have.
you could use google's geocoding to obtain coordinates of your post codes
EDIT: I don't really like you changing the sense of the question like that but ok. Please try sth like that (its not tested):
// Creating an array that will contain the coordinates
// for New York, San Francisco, and Seattle
var places = [];
// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var result;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=your+code&sensor=false",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
result = eval('(' + xmlhttp.responseText + ')');
if (result.status == "OK") {
var location = new google.maps.LatLng(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng);
places.push(location);
this should probably work, despite possible minor errors.
EDIT2: I have just now found simpler solution:
// Creating an array that will contain the coordinates
// for New York, San Francisco, and Seattle
var places = [];
// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': "your+code"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
//var marker = new google.maps.Marker({
//map: map,
//position: results[0].geometry.location
//});
places.push(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});