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 に答える