3

我已经为此苦苦挣扎了几个小时,我的问题是:

我在我的asp页面中创建了一个谷歌地图,我想通过点击地图在文本框中显示纬度和经度。

这是代码。

Javascript:

    var map;
    var marker = null;
    var latitudeTextBox = $("#<%= LatitudeTextBox.ClientID %>");
    var longitudeTextBox = $("#<%= LongitudeTextBox.ClientID %>");

function initialize() {

    var myCenter = new google.maps.LatLng(-33, 150.75);


    var mapOption =
    {
        center: myCenter,
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("googleMap"), mapOption);

    google.maps.event.addListener(map, 'click', function (event) {
        createNewMarker(event.LatLng,map);
    });

    google.maps.event.addDomListener(window, 'load', initialize);
}

function createNewMarker(location,map) {
    if (marker != null)
        marker.setMap(null);
    marker = new google.maps.Marker({
        position: location,
        animation: google.maps.Animation.BOUNCE,
        map: map,
    });

    var infowindow = new google.maps.InfoWindow({
        content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
    });

    infowindow.open(map, marker);

    latitudeTextBox.val(location.lat());
    longitudeTextBox.val(location.lng());
}

ASP:

<asp:TextBox id="LatitudeBox" runat="server" text=""></asp:TextBox>

<asp:TextBox id="LongitudeBox" runat="server" text=""></asp:TextBox>
4

2 回答 2

2
google.maps.event.addListener(marker, "drag", function(){
    document.getElementById("LatitudeBox").value=marker.getPosition().lat();
    document.getElementById("LongiitudeBox").value=marker.getPosition().lng();
});

标记拖动时将获得纬度和经度。您可以尝试您需要的事件。

请参阅此处了解更多信息

于 2013-10-04T14:19:44.613 回答
2

这是http://jsfiddle.net/Blunk/x8dSP/8/中Google Map API 3的演示。

注意:jQuery(document).ready在 jsfiddle 中使用来初始化地图。

在此处输入图像描述

<table>
    <tr>
        <td>Latitude:</td>
        <td>Longitude:</td>
    </tr>
    <tr>
        <td>
            <asp:TextBox runat="server" ID="LatitudeTextBox" Text="33.976222" /></td>
        <td>
            <asp:TextBox runat="server" ID="LongitudeTextBox" Text="-118.281698" /></td>
    </tr>
</table>
<div id="map_canvas" style="width: 300px; height: 300px">
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">

    var map;
    var marker;
    var latitudeTextBox = $("#<%= LatitudeTextBox.ClientID %>");
    var longitudeTextBox = $("#<%= LongitudeTextBox.ClientID %>");

    function initialize() {

        var centerLatlng = new google.maps.LatLng(latitudeTextBox.val(), longitudeTextBox.val());

        var mapOptions = {
            zoom: 10,
            center: centerLatlng,
            scrollwheel: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: true,
            mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DEFAULT },
            navigationControl: true,
            navigationControlOptions: { style: google.maps.NavigationControlStyle.DEFAULT }
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        marker = new google.maps.Marker({
            draggable: true,
            map: map,
            position: centerLatlng
        });

        google.maps.event.addListener(marker, 'dragend', function() {
            var curLatLng = marker.getPosition();
            latitudeTextBox.val(curLatLng.lat());
            longitudeTextBox.val(curLatLng.lng());
        });

        google.maps.event.trigger(marker, "click");

    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>
于 2013-10-04T16:29:31.800 回答