I amd che fa questa applcation android e sono un po 'bloccato con un problema. la cosa è che nella mia applicazione, una attività invia agli utenti la posizione corrente nel database, e usando questa stessa posizione faccio riferimento a un'altra tabella nel database in cui prendo questi valori, dove questa tabella è composta da altri utenti. così ora quello che voglio fare è usare la posizione attuale degli utenti e riferendomi agli altri utenti che la posizione vuole calcolare e che scegliere l'utente più vicino, sono andato avanti e ho creato un file .php. e quando eseguito sul browser web mi mostra l'ans corretta. ma non sembra funzionare nell'applicazione. attenzione che il file php tat fa questo computaton usa un javascript. questo è il file php che io chiamo nella mia attività asincrona che trova l'utente più vicino. il file phpquando si chiama un file php con un codice js da Android, il codice js è l'uscita di una tale chiamata
<?php
$response = array();
include 'db_con.php';
// run query
$query = mysqli_query($con,"SELECT *FROM friend WHERE d_status='0'");
// look through query
if (!empty($query)) {
// check for empty result
$response["details"] = array();
while($row =mysqli_fetch_assoc($query)){
$details= array();
$details["Latitude"]= $row["d_latitude"];
$details["Longitude"]= $row["d_longitude"];
array_push($response["details"], $details);
}
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
mysqli_close($con);
$lat=15.493403;
$longi=73.820617;
?>
<!DOCTYPE html>
<html>
<body>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2 /jquery.min.js"></script>
<script type="text/javascript">
var latlon = new Array();
var map;
var lat=15.493403;//"<?php echo $lat; ?>";
var longi=73.820617;//"<?php echo $longi; ?>";
var geocoder;
var origin =new google.maps.LatLng(lat,longi);
function mapLoad(){
var data="<?php echo $response;?>";
var data_parsed = JSON.parse(data);
var resp = data_parsed['details'];
for (var i in resp) {
latlon[i]=(resp[i].Latitude+","+resp[i].Longitude);
//alert("latit:"+latlon[i]);
}
calculateDistances();
}
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins:[origin], //array of origins
destinations: latlon, //array of destinations
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback); //agettaxicoor
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
//we only have one origin so there should only be one row
var routes = response.rows[0];
//need to find the shortest
var lowest = Number.POSITIVE_INFINITY;
var tmp;
var shortestRouteIdx;
var resultText = "Possible Routes: <br/>";
for (var i = routes.elements.length - 1; i >= 0; i--) {
tmp = routes.elements[i].duration.value;
resultText += "Route " + latlon[i] + ": " + tmp + "<br/>";
if (tmp < lowest) {
lowest = tmp;
shortestRouteIdx = i;
}
}
//log the routes and duration.
$('#results').html(resultText);
//get the shortest route
var shortestRoute = latlon[shortestRouteIdx];
var res = JSON.stringify(shortestRoute .split(","));
//alert(res);
location.href="atestb.php?Result=" + res; //this where the result is set.
}
}
</script>
<body onload="mapLoad();"></body>
</body>
</html>
ma il problema ora è quando uso lo strumento di debug in Eclipse vedo che l'intero codice dopo
DOCTYPE html
diventa l'output dell'attività async chiamata json.
quindi in pratica tutto ciò che voglio fare è chiamare questo file php dall'app, calcolare l'utente più vicino e restituire il valore di latitudine e longitudine dell'utente più vicino all'applicazione.maybe aggiornando lo stesso php variabile di file o aggiornamento di un'altra variabile di file php come il caso precedente so che questo potrebbe non essere il modo corretto per farlo, quindi qualsiasi aiuto sarebbe gud.
Posso chiederti perché usi PHP? Inoltre, penso che non puoi eseguire PHP su Android. Hai bisogno di apache + php installato, ecco perchè funziona sul computer, non su un androide. Se si desidera utilizzare un file php, inserirlo su un server quindi richiederlo dall'app, utilizzando ajax. (codice IE nell'app per Android non significa nulla) – Larta
scusa se non lo menziono, ma questo file si trova sul server. –