0

我正在按地址获取纬度和经度。一切正常,我可以毫无问题地显示地图,但我想显示高度和经度。

这是我的代码,我在正文中初始化

function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                draggable: true,
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
        google.maps.event.addListener(marker, 'click', function() {
            document.getElementById('lat').value = results[0].geometry.location.lat();
            document.getElementById('lng').value = results[0].geometry.location.lng();
        });
    });
}

这是用法:

<div id="mapholder"></div>
            </p>
            <div id="map_canvas"></div>
            <div id="info"></div>
            <div>
                <input id="address" type="textbox" value="הזן כתובת">
                <input type="button" value="מצא את מיקומך" onclick="codeAddress()"><br>
                <input type="button" value="מצא קופונים באזורך" onclick="showPositionCoupons(32.105892, 35.198483)"><br>
                Latitude: <input type="text" id="lat"><br>
                Longitude: <input type="text" id="lng"><br>
            </div>
4

1 回答 1

0

好吧,发现代码很好用

function updateCoordinates(latlng)
{
  if(latlng) 
  {
    document.getElementById('lat').value = latlng.lat();
    document.getElementById('lng').value = latlng.lng();
  }
}

function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            updateCoordinates(results[0].geometry.location);
            if (marker) marker.setMap(null);
            marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                draggable: true
            });

            google.maps.event.addListener(marker, "dragend", function() {
                updateCoordinates(marker.getPosition());
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
于 2012-08-25T22:54:48.573 回答