У меня есть код, в котором я рассчитываю расстояние от текущего местоположения, и он работает нормально, когда я ввожу адрес до области, но когда я даю полный адрес (с номером дома) то он не сможет найти адрес. однако, когда я пропускаю только область и город, тогда он работает нормально, но я не могу остановить конечного пользователя, чтобы дать только область и город. Как справиться с этой ситуацией.Полный адрес не удалось найти с использованием геолокации
Возможно ли, что код может игнорировать дом и поиск с использованием только области или города?
<html>
<%@include file="configuration_parameter.jsp" %>
<%
try
{
// HttpSession session_1 = request.getSession(false);
// String address = request.getParameter("full_address");
String address="132/4a,Makdikhera,KALYANPUR , KANPUR, UTTAR PRADESH";
String rs_creation_date=request.getParameter("rs_creation_date");
//String address="asdfasdfasfasdfasdf";
%>
<body onLoad="getLocation()">
<p id="demo"><font face=calibri color=blue>Please wait.. Distance is being calculated from your current location..</font></p>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=<%=Google_API_Map_key%>&sensor=false"></script>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var geocoder = new google.maps.Geocoder();
var address = "<%=address%>";
geocoder.geocode({ 'address': address}, function(results, status) {
if (status != google.maps.GeocoderStatus.OK) {
var xx = document.getElementById("demo");
xx.innerHTML = '<font face=calibri color=grey size=3>Posted by <%=rs_creation_date%> | </font><font face=calibri color=red size=3>Destination distance: Sorry, Unable to find out the destination distance.</font>';
}
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var R = 6371;
var dLat = (latitude-position.coords.latitude) * (Math.PI/180);
var dLon = (longitude-position.coords.longitude) * (Math.PI/180);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos((position.coords.latitude) * (Math.PI/180)) * Math.cos((latitude) * (Math.PI/180)) * Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
function displayLocation(latitude,longitude,distance,adr){
var request = new XMLHttpRequest();
var method = 'GET';
// var sourcelocation = "london";
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude;
var async = true;
request.open(method, url, async);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
var data = JSON.parse(request.responseText);
var address = data.results[0];
var xx = document.getElementById("demo");
var time = distance/50;
xx.innerHTML = '<font face=calibri color=grey size=3>Posted by <%=rs_creation_date%> | </font> <font face=calibri color=green size=3>'+distance+' Km away from your current location</font>';
}
};
var retresults = "Source Location :"+address.formatted_address+" Destination Address:"+adr+" Distance:"+distance+" km";
request.send();
return retresults;
};
var adr = address;
var home = displayLocation(position.coords.latitude,position.coords.longitude,d.toFixed(2),adr);
}
}
);
}
</script>
<%
} catch (Exception e)
{
out.println("<font face=calibri color=red>Oops. There is some problem to find out the address details</font>");
}
%>
</body>
</html>