简体   繁体   中英

how to load vb.net variable to javascript

I'm trying to add google maps to my website, but instead of fixing a latitude and longitude for the location, I would like to be able to load the values from database. How do I get a variable from vb.net and pass it to javascript? I've tried:

<% Response.Write("<script>
    function initialize() {
        var myLatlng = new google.maps.LatLng(1.123456, 1.123456);
        var mapOptions = {
            zoom: 16,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'KH Automotive Location'
        });
    }

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

</script>")%>

and

new google.maps.LatLng(<%=latitude%>, <%=longitude%>);

Please help. Thanks

Sometimes, when I need to make absolutely sure (like with translated/localized strings) that the content comes across, I'll write it out to a hidden DIV somewhere on the page (or a hidden INPUT).

<div style='display:none' id='my_data'>stuffstuffstuff</div>

Then I just read the contents of the DIV (or INPUT, whatever I used). Slap a "runat=server" in it, and be able to write to it from your code-behind.

Should be able to write it in:

<% Response.Write("..
   ..
   var myLatlng = new google.maps.LatLng(" & latitude & ", " & longitude & ");
   ..
")%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM