从GMAP的另一个HTML元素获取坐标

时间:2015-12-05 19:27:51

标签: javascript google-maps google-maps-api-3

到目前为止,我一直在尝试在GMaps上实现飞机旅行的模拟。但是我遇到了一个我无法弄清楚的错误或逻辑(无论你怎么称呼)。

在我的项目中,我做了一个数据库连接以获得Runway坐标,然后我成功收到并让他们写入标签后我尝试通过javascript到达标签再次获得坐标以进入GMaps但它没有工作。(地图不开放) 我会非常高兴,如果你能帮助我的话。

先谢谢

<script>
    var latstrt = 0;
    var longtstrt = 0;
    function Load()
    {
        latstrt = document.getElementById("<%=Label6.ClientID %>").innerHTML;
        longstrt = document.getElementById("<%=Label7.ClientID %>").innerHTML;

        window.alert(latstrt);
    }


    window.onload = function () {
        Load();
        initialize();
    };


window.alert(latstrt);
    var myCenter = new google.maps.LatLng(latstrt,longtstrt); 
    var elaziz = new google.maps.LatLng(38.608334, 39.291668);
    function initialize() {

        var mapProp = {
            center: myCenter,
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };



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

        var marker = new google.maps.Marker({
            position: myCenter,
            draggable: true,
        });
        var myTrip = [myCenter,elaziz];
        var flightPath = new google.maps.Polyline({
            path: myTrip,
            strokeColor: "#0000FF",
            strokeOpacity: 0.8,
            strokeWeight: 2
        });
        marker.setMap(map);

        flightPath.setMap(map);


    }




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


</script>

1 个答案:

答案 0 :(得分:0)

以下是您的代码的略微修改版本:

function initialize() {
    var latstrt = document.getElementById("<%=Label6.ClientID %>").innerHTML;
    var longstrt = document.getElementById("<%=Label7.ClientID %>").innerHTML;

    var myCenter = new google.maps.LatLng(latstrt,longtstrt); 
    var elaziz = new google.maps.LatLng(38.608334, 39.291668);

    var mapProp = {
        center: myCenter,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.HYBRID
    };

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

    var marker = new google.maps.Marker({
        position: myCenter,
        draggable: true,
        map: map
    });
    var myTrip = [myCenter,elaziz];
    var flightPath = new google.maps.Polyline({
        path: myTrip,
        strokeColor: "#0000FF",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        map: map
    });
}

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