我喜欢传递一个JS变量' latlng'到一个php变量,但它不起作用。在这段时间里,它运作良好。代码末尾的php变量返回' [object HTMLSpanElement]'而且corret是我当前的位置
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;
function initialize() {
var myOptions = {
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latlng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
map.setCenter(latlng);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
setupEvents();
centerChanged();
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
function setupEvents() {
reverseGeocodedLast = new Date();
centerChangedLast = new Date();
setInterval(function() {
if ((new Date()).getSeconds() - centerChangedLast.getSeconds() > 1) {
if (reverseGeocodedLast.getTime() < centerChangedLast.getTime())
reverseGeocode();
}
}, 1000);
google.maps.event.addListener(map, 'zoom_changed', function() {
document.getElementById("zoom_level").innerHTML = map.getZoom();
});
google.maps.event.addListener(map, 'center_changed', centerChanged);
google.maps.event.addDomListener(document.getElementById('crosshair'), 'dblclick', function() {
map.setZoom(map.getZoom() + 1);
});
}
function getCenterLatLngText() {
return '(' + map.getCenter().lat() + ', ' + map.getCenter().lng() + ')';
}
function centerChanged() {
centerChangedLast = new Date();
var latlng = getCenterLatLngText();
document.getElementById('latlng').innerHTML = latlng;
document.getElementById('formatedAddress').innerHTML = '';
currentReverseGeocodeResponse = null;
}
function reverseGeocode() {
reverseGeocodedLast = new Date();
geocoder.geocode({
latLng: map.getCenter()
}, reverseGeocodeResult);
}
function reverseGeocodeResult(results, status) {
currentReverseGeocodeResponse = results;
if (status == 'OK') {
if (results.length == 0) {
document.getElementById('formatedAddress').innerHTML = 'None';
} else {
document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
}
} else {
document.getElementById('formatedAddress').innerHTML = 'Error';
}
}
function geocode() {
var address = document.getElementById("address").value;
geocoder.geocode({
'address': address,
'partialmatch': true
}, geocodeResult);
}
function geocodeResult(results, status) {
if (status == 'OK' && results.length > 0) {
map.fitBounds(results[0].geometry.viewport);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}
function addMarkerAtCenter() {
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map
});
var text = 'Lat/Lng: ' + getCenterLatLngText();
if (currentReverseGeocodeResponse) {
var addr = '';
if (currentReverseGeocodeResponse.size == 0) {
addr = 'None';
} else {
addr = currentReverseGeocodeResponse[0].formatted_address;
}
text = text + '<br>' + 'address: <br>' + addr;
}
var infowindow = new google.maps.InfoWindow({
content: text
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
</script>
<body onLoad="initialize()">
<h1 style="display:none;">APP Google Maps - Localizar Latitude / Longitude</h1>
<div style="position: relative; width:980px; left:50%; margin-left:-490px; height:90px;">
<div style="position:absolute; left:0px; top:20px;">
</div>
<div style="position:absolute; right:0px; top:30px;">
Localizar Região:
<input type="text" class="form_contato" id="address" style="width:300px; margin-right:15px;" />
<input type="button" value="Procurar" onClick="geocode()" class="form_contato">
<input type="button" value="Adicionar ponto no mapa" onClick="addMarkerAtCenter()" class="form_contato" />
</div>
</div>
<div style="position: relative; width:980px; left:50%; margin-left:-490px; height:40px; line-height:40px; text-align:center;" class="Caecilia17">Para realizar a pesquisa digite no formulario acima o endereço desejado.</div>
<div style="background:#ffffff;">
<div id="map">
<div id="map_canvas" style="width:100%; height:500px"></div>
<div id="crosshair"></div>
</div>
</div>
<div style="position: relative; width:980px; left:50%; margin-left:-490px; height:90px; margin-top:20px; line-height:20px;" class="arial15cinza">
Latitude / Longitude: <span id="latlng"></span>
<br />Endereço: <span id="formatedAddress"></span>
<br />Nivel do zoom: <span id="zoom_level"></span>
<br />
<?php
//escrevendo a variável JS pelo PHP
$latlng = echo "<script>document.write(latlng);</script>"
?>
</div>
</body>
答案 0 :(得分:0)
一种解决方案是通过查询字符串将javascript变量传递给PHP页面。 你可以这样做:
var newVar = "somevalue";
window.location.href = "http://stackoverflow.com?variable=" + newVar;
然后在你传递给它的php页面中抓住它,就像这样
$phpVariable = $_GET['variable'];
我希望这有帮助,也可以谷歌服务器端与客户端编码,以帮助更好地了解他们如何互动