将Google地图与ASP.NET网站集成

时间:2012-09-22 17:24:38

标签: javascript asp.net google-maps-api-3

嗨,是的,我已经与Google文档进行了斗争,不,我没有要求任何人为我写FoC申请!我做了很多SQL论坛在MSDN上回答自己,并了解论坛是如何工作的,可能只是非常糟糕地问这个问题。我很感激你指出这一点,希望它会带来更多回答问题的机会。如上所述,我真的只是希望有人可以发布我正在讨论的事情的一些工作样本,然后我可以修补。到目前为止,我所拥有的代码如下,但处于某种状态。将参数传递给Javascript是行不通的,我无法弄清楚如何开始让它接受用户与地图的交互作为输入。我的代码基于来自论坛帖子的示例,因为我发现这比官方Google文档更有用!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
 <html>
       <head> 
               <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
               <title>Calculate Distance</title>
                <script type="text/javascript"    
                             src="http://maps.google.com/maps/api/js?sensor=false"></script>

       </head>
       <body style="font-family: Arial; font-size: 13px; color: red;">
           <form runat="server">
                <div id="map" style="width: 400px; height: 300px;"></div>
                 <script type="text/javascript">
                     var startlocation = document.getElementById('Start').textcontent;
                     var endlocation = document.getElementById('End').textContent;
                     var directionsService = new google.maps.DirectionsService();
                     var directionsDisplay = new google.maps.DirectionsRenderer();
                     var myOptions = 
                         {      zoom:7,      mapTypeId: google.maps.MapTypeId.ROADMAP    }     
                     var map = new google.maps.Map(document.getElementById("map"), myOptions);
                     directionsDisplay.setMap(map);
                     var request = 
                         {        
                             origin:  startlocation,
                             destination: endlocation,
                             travelMode: google.maps.DirectionsTravelMode.DRIVING  
                         }; 
                     directionsService.route(request, function(response, status) { 
                         if (status == google.maps.DirectionsStatus.OK) { 
                             // Display the distance:
                             document.getElementById('Distance').innerHTML += 
                                 response.routes[0].legs[0].distance.value / 1609.344 + " miles";
                             directionsDisplay.setDirections(response);
                         }
                     });

                 </script>
           <asp:Label ID="Distance" runat="server" Text="Distance: "></asp:Label>
           <asp:TextBox ID="Start" runat="server" Text="hedge end"></asp:TextBox>
           <asp:TextBox ID="End" runat="server" Text="fareham"></asp:TextBox>
               <asp:Button ID="CalcDistance" runat="server" Text="Button" />
            </form>
       </body>  

 </html> 

我是javascript的新手,对ASP.NET来说还是新手,我几天都在摆弄它,无法到达任何地方。

我想将Google地图集成到ASP.NET页面中,以便用户可以选择点击地图上的2个点,或者选择将一个或两个地址插入文本框。

一旦在地图上输入或绘制了两个位置,我就需要以英里为单位返回最短的行车距离。

如果有人可以通过发布此类或类似的工作样本来帮助我,我会非常感激。

非常感谢您的帮助。

皮特

1 个答案:

答案 0 :(得分:1)

Start with this tutorial.

Documentation is here.

How to calculate distance is here.

编辑:

这是Google Maps API v3和ASP.NET的距离计算示例。

客户代码:

<!DOCTYPE html>
<html>
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Calculate Distance</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>
    <style type="text/css">
        #map{width:800px;height:500px}
    </style>

</head>

<body style="font-family: Arial; font-size: 13px; color: red;">
<form id="Form1" runat="server">

    <div id="map"></div>

    <input runat="server" type="hidden" id="DistanceValue" name="DistanceValue" />

    <script type="text/javascript">

        var latlng = new google.maps.LatLng(54.40708, 18.667485);
        var latlng2 = new google.maps.LatLng(54.40708, 18.667485);

        var myOptions =
            {
            zoom:4,
            center:latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            };

        var map= new google.maps.Map(document.getElementById('map'),myOptions);

        var marker = new google.maps.Marker({
            position: latlng,
            title: "Westerplatte - first battle of WW2 in Europe",
            clickable: true, 
            map: map
        });

        var marker2 = new google.maps.Marker({
            position: latlng2,
            title: "Westerplatte - first battle of WW2 in Europe",
            clickable: true,
            map: map
        });

        google.maps.event.addListener(map, "click", function (event) {
            latlng2 = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
            marker2.setMap(null);
            marker2 = new google.maps.Marker({
                position: latlng2,
                title: "selected by user",
                clickable: true,
                map: map
            });

            var hidden = document.getElementById("DistanceValue");
            hidden.value = google.maps.geometry.spherical.computeDistanceBetween(latlng, latlng2) / 1000;
        });

    </script>


    <asp:Button  ID="Button1" runat="server" Text="Send distance" onclick="Button1_Click" />

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</form>
</body>  

</html> 

服务器代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.Form["DistanceValue"] != null)
        {
            string myHiddenFiledValue = Request.Form["DistanceValue"].ToString();
            Label1.Text = myHiddenFiledValue.Split(',','.')[0] + " [km]";
        }
    }