I'm using Google Maps Geocoding function. I got some addresses which I load into a PHP variable. The addresses are defined by a textbox which the user can type in a zip code. I give an example:
A user types in the textbox a postal code (12345 for example). Now it loads addresses from the system (this is internal - but it works to load the addresses). And then it display some addresses. These addresses are now stored in a PHP variable ($address). And now I want to get the latitude and longitude from these different addresses.
I had a look at this thread: Javascript geocoding from address to latitude and longitude numbers not working
I used this code from the article:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude + ' - ' + longitude);
This is nice but it just give me the latlng from the address I typed in the textbox, not from all the addresses I got.
Is there a way to get the latitude and longitude from all the different addresses? If yes, could someone please explain me? I'm new to JavaScript and Google Maps API.
Thanks in advance
You can use the example given here.
See the code below for specific functionality:
geocoder = new google.maps.Geocoder();
function findAddress(address)
{
// You can pass the address from your PHP to Javascript using AJAX. Here, I'm assuming that you have done it and that you're passing it as a string argument to this function
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
return position: results[0].geometry.location;
}
else
{
// Show some message that the address cannot be found on map.
}
});
}
Related
I am working on JavaScript.I have places in array format like ["Chennai","Hyderabad","Bangalore",....] I want to find latitude and longitude of them .
As per I know we can I find one place details at a time using geocode.but I want to find bunch of place details.For this I read Google batch request(link) concept, I didn't understand.
I can make API request one by one but it's not a good practice.Even If I follow same way I will get limitation error from Google.
can anyone help me, How can I find latitude and longitude details.
If anyone wants to down-vote, please let me know the reason.
Thanks.
Use the Geocoder:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address },
function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.log('lng: ' + location. getLng() + ', lat: ' + location.getLat());
}
});
Note that you can't do a batch request with the geocoder API. But you can use the directions api to geocode up to 8 addresses. In the business class it allows up to 24 addresses. Read about it:
https://developers.google.com/maps/documentation/javascript/directions
I have a little hacked-together web app that uses the google maps api to draw data from a database and create and display markers and associated data on an embedded map, using JSON objects. I'm accessing the api here:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
I've now realized I need a search box on the map, so users search for an address and the map will shift to display their desired location. Seems like I need the Places library, I can really easily implement a search box using the code samples in the documentation. I don't care about the Places themselves or doing anything with them, just identifying the location and shifting the resetting the bounds to that desired location. So I need to access the places library, which apparently is here:
https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false
BUT, my previous code (perhaps not surprisingly) breaks if I switch out the libraries. Is there any way to use both? Do I have to deal with Places is some other way because I'm already using JQuery for this app? Or is it just a problem with how I'm referring to the libraries?
Thanks! z
Simply Do this
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("txtAddress").value;
var lat;
var lng;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
lat= results[0].geometry.location.latitude;
lng =results[0].geometry.location.longitude;
}
});
map.panTo(new google.maps.LatLng(lat,lng));
map.setZoom(14);
I'm trying to use the geocoder to get co-ordinates for the following address:
1945 Barton Street, Hamilton, ON, L8H2Y7
Searching the address on google maps itself finds the address: http://g.co/maps/5axb5.
Using the geocode url also returns fine. http://maps.googleapis.com/maps/api/geocode/json?address=1945+BARTON+STREET,+HAMILTON,+ON,+L8H2Y7&sensor=false
However, when I use the api (my code below), it returns not found.
Code: (this works fine for several other addresses I'm searching)
if (geocoder) {
geocoder.geocode( {'address': fullAddress }, 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
});
} else {
alert("Geocoding failed: " + status + " " + fullAddress);
}
});
}
The "actual" address is "1945 Barton Street East, Hamilton, ON L8H2Y7, Canada", but maps and the direct url still find the place fine, only the geocoder doesn't.
Any ideas as to why this might be happening?
EDIT:
I figured it out. It was a different address that it legitimately couldn't find (not on google maps or using the geocoder), but because the request was being done asynchronously by the time the geocoder realized it couldn't find the address, the loop had already changed the address value to another address which it could find. Seems like I've got some work ahead of me..
it's strange because it work's on my live demo check this
Live Demo
Do you have more code somewhere??
Update
I test the service with the plus symbol, and even without canada and still worked
here's examples
trying to add 45 different points to a map, but I just get OVER_QUERY_LIMIT returned, and no map.
I am getting locations via a string search, then assigning the returned LatLng object to a new google Marker object
String Search
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': x}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var result = results[0].geometry.location;
return result;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
Google Geocoding is rate limited throughout the day. It's also limited by IP address. So, two possible interactions: If you're sharing an IP address with lots of other people, they may also be dragging down your quota. This is particularly true in mobile environments. Another more likely possibility is that you are running into the rate limit over the day. If you get that, pause of a second or two and try again.
You can also cache geocodes and addresses for performance purposes, and have a back-up server side geocoding service using Google's web services. That'll also be quota'd but it's a good fallback.
Consider using Google Fusion Tables for the data storage and its geocoding part- at the very least, their batch geocoding interface obeys their own internal limits.
http://support.google.com/fusiontables/bin/answer.py?hl=en&answer=1012281
How you should approach this is by geocoding the static address, if they are static and save the lat lng for further use. I don't know what to say if there not static tho.
I am wondering if it possible to use Bing Maps API to perform geocoding (get long/lat based on Postal Code or address) as a batch process or at least without having to render a map with each geocode. With google maps API, the process looks like this
var geocoder=new google.maps.Geocoder();
geocoder.geocode({ 'address': pCodes[counter-1] }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK){
var pcode = results[0].address_components[0].long_name;
var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
// do something with results
}
else {
// error condition
}
});
With Bing the VEMap map class, it looks like map and geocoding are bound together.
You are correct, the VEMap is for displaying maps.
If you just want location data, you can call the Bing Maps REST based Location API.
You can find out more about the API on MSDN at http://msdn.microsoft.com/en-us/library/ff701714.aspx