Trying to obtain latitude and longitude using AJAX JSONP.
Here is the code snippet:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
$(document).ready(function(){
var region = "Rajkot,Jamnagar";
var cn ="IN";
var lat;
var lng;
var array = region.split(',');
var lat = new Array(), lng = new Array();
var totalLength = array.length;
var count = 0;
for(var item in array)
{
$.ajax({
url: "http://services.gisgraphy.com//geocoding/geocode?address="+array[item]+"&country="+cn+"&format=json",
async: false,
dataType:'jsonp',
success: function(data){
count++;
lat.push(data.result[0].lat);
lng.push(data.result[0].lng);
if (count == totalLength) {
var commaSeperatedLat = lat.toString();
localStorage.setItem("newlat",commaSeperatedLat);
var commaSeperatedLong = lng.toString();
console.log(commaSeperatedLat);
}
}
});
}
// Function to retrieve latitude and longitude when button is clicked
function getAllData()
{
var moodle_groups_id = (localStorage.getItem("newlat"));
alert(moodle_groups_id);
}
});
</script>
<body>
<input type="button" value="Retrieve Data" onclick="getAllData()"></input>
</body>
Successfully fetching all latitude and longitude data.
Now, looking to access these variables on button click event using Local Storage methods.
Please assist me in resolving this issue.
Thank you.