am working android project using web languages on android webview. I tried to calculate the distance between a user location and other places location, but it keeps giving me large values and the same time, the same value of distance for all the places search result. I tested other application result, but my own seems different. Every other thing works fine, except the value of the distance which is the same for all places irrespective of there distance, and always different from the value other similar apps from Google pay store give. My code is below for assistance.
var map, places, infoWindow;
var geocoder;
var pos;
function initialize() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 10,
mapTypeControl: false,
panControl: false,
zoomControl: false,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
infoWindow = new google.maps.InfoWindow({
content: document.getElementById('info-content')
});
// Create the autocomplete object and associate it with the UI input control.
// Restrict the search to the default country, and to place type "cities".
places = new google.maps.places.PlacesService(map);
google.maps.event.addListener(map, 'tilesloaded', function() {
document.getElementById("loading").style.display = "none";
});
// Add a dom event listener to react when the user clicks the search button
google.maps.event.addDomListener(document.getElementById('button'), 'click',
onPlaceChanged);
}
function distance () {
places.textSearch(search, function(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var service5 = new google.maps.DistanceMatrixService();
service5.getDistanceMatrix(
{
origins: [pos],
destinations: [results[i].geometry.location],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
function callback(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK) {
document.getElementById('iw-distance1').textContent = response.rows[0].elements[0].distance.text;
document.getElementById('iw-time').textContent = response.rows[0].elements[0].duration.text;
}
else {
alert ("helo");
}
}
}
}
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
map.setCenter(options.position);
}
<tr id="iw-website-row" class="iw_table_row">
<td class="iw_attribute_name">distance:</td>
<td id="iw-distance1"></td>
</tr>
<tr id="iw-website-row" class="iw_table_row">
<td class="iw_attribute_name">time:</td>
<td id="iw-time"></td>
</tr>
google.maps.event.addDomListener(window, 'load', initialize);