역 지오 코딩을 사용하여 경도 및 위도 좌표를 기반으로 Google지도에 마커를 배치하려면 Google Maps API3을 사용하고 있습니다.Google지도 지오 코드 위치를 PHP 변수로 저장하려고합니다.
위도와 경도에 따라지도에서 주소를 찾아 잘 표시합니다. 그러나, 나는 그 구체적인 주소를 다시 PHP 측에 보내야합니다. 그리고 나는 어떻게 몰라? 나는 Ajax 요청을 사용하려고 시도했지만 잘못하고 있다고 생각한다.
이
함수는 (은 .js) 모습입니다 :function geocodeLatLng(geocoder, map, infowindow)
{
result.forEach(function(entry) {
var latlng = {lat: parseFloat(entry.lat), lng: parseFloat(entry.lng)};
geocoder.geocode({'location': latlng}, function(results, status)
{
if (status === google.maps.GeocoderStatus.OK)
{
if (results[1])
{
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
// send som data
var xhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var PageToSendTo = "article.php?";
var markerLocation = results[1].formatted_address;
var VariablePlaceholder = "location=";
var UrlToSend = PageToSendTo + VariablePlaceholder + markerLocation;
xmlhttp.open("GET", UrlToSend, false);
xmlhttp.send();
}
else
{
window.alert('No results found');
}
}
else
{
window.alert('Geocoder failed due to: ' + status);
}
});
});
}
var에 markerLocation 내가 PHP 변수에 저장해야 할 데이터가 들어있는,하지만 난 (가로 질러 활용하는 방법을 잘 모릅니다 js에서 PHP로) 올바르게.
그리고 이것은 서버 측 PHP 파일 (article.php)입니다 :
<?php
require_once 'config.php';
require_once 'src/articleLogic.php';
$id = $_GET['id'];
$sql = "SELECT * FROM blog WHERE id=$id";
$result = db_select($sql);
?>
<script type="text/javascript">
// pass PHP variable declared above to JavaScript variable
var result = <?php echo json_encode($result) ?>;
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script type="text/javascript" src="js/map_article.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=myAPIkey&callback=initMap"></script>
<body>
<?php echo $navbar; ?>
<div id="map">
</div>
<?php getArticle(); ?>
<div id="photos">
<?php getFlickrPhotos(); ?>
</div>
<?php
// Testing the jquery get-method
if(isset($_GET['location']))
{
echo $_GET['location'];
}
else
{
echo "It didnt work";
}
?>
</body>
</html>
편집 : 코드 결함 ajaxrequest 업데이트되었습니다.
시도한 AJAX 요청을 볼 수 있습니까? 우리가 네가 잘못한 곳을 볼 수 있다면이 모든 것을 다 해결할 수 있도록 도와 줄 수 있다고 생각한다. – Ohgodwhy
예, 제 잘못입니다. 함수에서 AJAX 요청에 대한 시도를 추가했습니다. 의견 아래 // 일부 데이터를 보냅니다. 그리고 콘솔에서 알려주는 내용입니다. 메인 스레드의 동기 XMLHttpRequest는 최종 사용자 경험에 부정적인 영향을주기 때문에 더 이상 사용되지 않습니다. 도움이 더 필요하면 https://xhr.spec.whatwg.org/를 확인하십시오. – Stim