在 Google 地图中,event.latlng.lat() 将地理纬度/经度表示为十进制数。我需要将数字 53.5030167、8.0528333 格式化为字符串 N 53°30.181' E 008°03.170'。不幸的是,下面的代码不起作用。我的错误在哪里?
<script>
var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(54, 8),
zoom: 14,
maxZoom: 21,
mapTypeId: google.maps.MapTypeId.TERRAIN,
panControl: false,
draggableCursor: "default",
streetViewControl: false
});
google.maps.event.addListener(map,'click',function(event) {
document.getElementById('latlngclick').value = dec2StrLat(event.latLng.lat()) + ', ' + dec2StrLng(event.latLng.lng())
})
google.maps.event.addListener(map,'mousemove',function(event) {
document.getElementById('latspan').innerHTML = dec2StrLat(event.latLng.lat())
document.getElementById('lngspan').innerHTML = dec2StrLng(event.latLng.lng())
});
google.maps.event.addDomListener(window, 'load', initialize);
function dec2StrLat(decLatitude) {
var intDegree;
var decMinute;
var strLatitude = "N";
if (decLatitude < 0) {
strLatitude = "S";
decLatitude = decLatitude * -1;
}
intDegree = Math.floor(decLatitude);
decMinute = (decLatitude - intDegree) * 60;
decMinute = myRound(decMinute, 3);
strLatitude = strLatitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
return strLatitude
}
function dec2StrLng(decLongitude) {
var intDegree
var decMinute
strLongitude = "E";
if (decLongitude < 0) {
strLongitude = "W";
decLongitude = decLongitude * -1;
}
intDegree = Math.floor(decLongitude);
decMinute = (decLongitude - intDegree) * 60;
decMinute = myRound(decMinute, 3)
strLongitude = strLongitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
return strLongitude
}
</script>