我有一个可以运行的google map脚本,但是我不知道如何将其添加到我的asp.net内容页面。将脚本添加到页面的正确方法是什么?

看到代码:

 <%@ Page Title="Our Location" Language="VB"     MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="Location.aspx.vb" Inherits="About_Location" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

<script type="text/javascript"
  src="http://maps.google.com/maps/api/js?sensor=false">
</script>

<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
##map{ height: 100% }
</style>

</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server"    OnLoad="codeAddress()">

<script type= "text/javascript">

var geocoder;
var map;
var marker;

function codeAddress() {
    alert("hello")


    //initializes the map

    //create geocoder
    geocoder = new google.maps.Geocoder();
    //set options for the map being created
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    //create map instance and pass it the myOptions object literal
    map = new google.maps.Map(document.getElementById("map"), myOptions);

    //geocode to get the lat and lng points of the given address
    geocoder.geocode({ 'address': 'New York, New York'}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            //if geocoder finds location
            //center the map on the latlng points of the given address
            map.setCenter(results[0].geometry.location);
            map.setOptions({ zoom: 18 });
            //create marker and place it on the address
            marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: 'New York, New York'
            });
        }
        else {
            //if geocoder cannot find location, alert user
            alert("Could not find location");
        }
    });




}





</script>

<div id="map" style="width:700px; height:400px;"></div>

</asp:Content>

最佳答案

将您的脚本放在单独的js文件中,像使用Google Maps脚本一样进行声明。然后,您必须调用它,也许将此“#map” div id作为参数发送给该脚本,而不是在javascript中进行硬编码。

在这种情况下,您想使用脚本管理器在页面加载中执行以下操作:

ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowGoogleMap", "codeAddress('map');", true);

顺便提一句。对于Google Maps,您可以使用Google Maps服务器控件,从服务器端使用管理 map 要容易得多:

http://googlemap.codeplex.com/

10-04 23:09
查看更多