/    Sign up×
Community /Pin to ProfileBookmark

Need help with Google API LatLng values?

I have an array where I store location addresses and the array contains the following values:

217 Pacific Avenue,Toronto,M6P 2P7,Ontario
20 Eglinton Avenue West Suite 2100, Box 2014,Toronto,M4R 1K8,Ontario
45 Sunrise Ave Apt # 415,Toronto,M4A2S3,Ontario

I am trying to get LatLng values for each value but I am always getting (43.6594037, -79.4659595) value for all values in the array.

[CODE]for(var i = 0; i < AddressArray.length; i++)
{
geocoder = new google.maps.Geocoder();

if(geocoder)
{
geocoder.geocode({‘address’ : AddressArray[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
alert(results[0].geometry.location);
} else
{
alert(“Geocode was not successful for the following reason: ” + status);
}
});
}
}[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@007JulienAug 13.2013 — You have probably to wait for the response for each address with something like this :
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Geocoding service</title>
<style type="text/css">

</style>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

</head>

<body>

<div id="pge">
<div id="rsp"></div>

</div>

<script type="text/javascript">
var arrAdd=["217 Pacific Avenue,Toronto,M6P 2P7,Ontario","20 Eglinton Avenue West Suite 2100, Box 2014,Toronto,M4R 1K8,Ontario","45 Sunrise Ave Apt # 415,Toronto,M4A2S3,Ontario"];
var nos=0; [COLOR=#0000ff]// A global variable[/COLOR]
var geocoder = new google.maps.Geocoder();
function cdeAdd() {
geocoder.geocode( {'address': arrAdd[nos]},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById('rsp').innerHTML+='<p>'+JSON.stringify(results)+'</p>';
if (++nos<arrAdd.length) cdeAdd();[COLOR=#0000ff]// A new call, if any, after the response[/COLOR]
}
else {alert('Geocode was not successful for the following reason: ' + status);}

});
}
cdeAdd();
</script>
</body>
</html>[/CODE]
I hope you will find latitude and longitude in the many fields of the response...

EDIT : Its possible to provide :
[CODE]
var str=JSON.stringify(results,'','<br>').replace(/(?:<br>)+/g,function(a){return '<br>'+(new Array(a.length>>2).join('&nbsp;&nbsp;&nbsp;'))});
document.getElementById('rsp').innerHTML+='<p>'+str+'</p>';
[/CODE]
×

Success!

Help @asifakhtar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...