单击地图后,我需要获取地址和坐标,我使用的是 google map api v3,我在输入中恢复了坐标,但我还需要该点的地址。
function initialize() {
var myOptions = {
center: new google.maps.LatLng(36.835769, 10.247693 ),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
var marker;
function placeMarker(location) {
if(marker){ //on vérifie si le marqueur existe
marker.setPosition(location); //on change sa position
}else{
marker = new google.maps.Marker({ //on créé le marqueur
position: location,
map: map
});
}
latitude.value=location.lat();
longitude.value=location.lng();
}
}
我使用这个 php 脚本来获取地址,但如果不知道如何在 javascript 中使用它?我可以使用javascript获取地址吗?
<?php
$url = 'http://where.yahooapis.com/geocode?location=40.714224,-73.961452&gflags=R&flags=J';
$response = json_decode(file_get_contents($url));
$location = $response->ResultSet->Results[0];
foreach ((array) $location as $key => $value) {
if($key == 'city')
$city = $value;
if($key == 'country')
$country = $value;
if($key == 'street')
$street = $value;
}
echo "Street: ".$street."<br>";
echo "City: ".$city;
echo "<br/>Country: ".$country;
?>