0
私は緯度と経度の値に変換したpg_queryから返されたアドレスを持っています。私はそれらの値をGoogleマップ上にマップマーカーを配置するために使用するjavascriptを持っています。 JSここJavaScriptでpg_queryを実行する際のPHP配列のJSONエンコーディングエラー
$arr = array();
while ($markers = pg_fetch_row($address)){
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$markers[0]."&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status=="OK") {
$arr = array();
$Lat = (float)$xml->result->geometry->location->lat;
$Lng = (float)$xml->result->geometry->location->lng;
$arr[] = array("lat" => $Lat, "lng" => $Lng);
}
echo json_encode($arr);
されています:
VM884:1 Uncaught SyntaxError: Unexpected token [ in JSON at position 35
at JSON.parse (<anonymous>)
at XMLHttpRequest.xhr.onreadystatechange (map.php:37)
JSONエンコードされたデータは、次のとおりです:ここで
[{"lat":43.66852,"lng":-70.245084}][{"lat":43.66852,"lng":-70.245084}]
はPHPで私は次のエラーを取得しています
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: 39.350033, lng: -94.6500523}
});
// Do not actually need labels for our purposes, but the site_id is best if they are required.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
console.log(xhr.responseText);
if (xhr.readyState == 4) {
var arr = JSON.parse(xhr.responseText);
console.log(xhr.responseText);
if(Array.isArray(arr)){
showMarkers(arr);
}
}
}
xhr.open('GET', 'markers.php', true);
xhr.send();
function showMarkers(locations){
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
var markerCluster = new MarkerClusterer(map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'});
}
}
右、どのように私はその結果を得るのですか? – KevMoe
$ arr = array(); while($ markers = pg_fetch_row($ address)){ ... } echo json_encode($ arr); 中断の最後に注意してください – daheda
ありがとうございます...ループの外側にエコーを移動すると、最初のレコードのみが表示されます。 – KevMoe