2013-01-22 199 views
0

我在asp.net中執行谷歌地圖API有問題。這裏是我的aspx代碼:谷歌地圖Api不顯示圖像

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="visual_loc.aspx.cs" Inherits="visual_loc" %> 



<%--A sample project by Ghaffar khan--%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
    <title>Your Data on Google Map </title> 
    <%--Google API reference--%> 
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false 
       &amp;key=asdfg" type="text/javascript"> 
    </script> 
</head> 

<body onload="initialize()" onunload="GUnload()"> 
    <form id="form1" runat="server"> 
    <asp:Panel ID="Panel1" runat="server"> 
       <%--Place holder to fill with javascript by server side code--%> 
       <asp:Literal ID="js" runat="server"></asp:Literal> 
       <%--Place for google to show your MAP--%> 
       <div ID="map_canvas" style="width: 100%; height: 728px; margin-bottom:      2px;"> 
       </div> 
       <br /> 
      </asp:Panel> 
     <br /> 
    </form> 
    </body> 

</html> 

而且我後面的代碼:
getLocation()是後者從我的數據庫中的經度和緯度的方法。 createDataTable()是從這些位置創建DataTable的方法。

protected void Page_Load(object sender, EventArgs e) 
    { 
     string user_id; 
     user_id = Request.Cookies["cookie"]["Login"]; 
     getLocation(user_id); 
     BuildScript(createDataTable()); 

    } 
    private void BuildScript(DataTable tbl) 
    { 
     String Locations = ""; 
     foreach (DataRow r in tbl.Rows) 
     { 
      // bypass empty rows   
      if (r["Latitude"].ToString().Trim().Length == 0) 
       continue; 

      string Latitude = r["Latitude"].ToString(); 
      string Longitude = r["Longitude"].ToString(); 

      // create a line of JavaScript for marker on map for this record  
      Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));"; 
     } 

     // construct the final script 
     js.Text = @"<script type='text/javascript'> 
          function initialize() { 
           if (GBrowserIsCompatible()) { 
           var map = new GMap2(document.getElementById('map_canvas')); 
           map.setCenter(new GLatLng(52.259, 21.012), 2); 
           " + Locations + @" 
           map.setUIToDefault(); 
           } 
          } 
          </script> "; 
    } 

整個操作導致顯示沒有內容的空白網站。我究竟做錯了什麼?

+0

這可能是在你的情況矯枉過正,但我​​發現通過JSON反饋位置是一個更乾淨的方式來做到這一點,並提供良好的定製。如果感興趣,我可以添加代碼。 – MikeSmithDev

+0

你在localhost上還是在域上運行這個?如果在某個域上,您確實有相應的密鑰? https://developers.google.com/maps/signup自2010年5月以來,** v2 API **已被棄用**。 – Jacco

+0

是的,我確實有有效的鑰匙,而且我在本地主機上託管它,邁克你能告訴我另一種選擇嗎?Ghaffar汗 – Grzzzzzzzzzzzzz

回答

0

問題是我做了錯誤的目錄到我的標記,所以無法在地圖上看到。一旦我輸入有效的目錄一切正常。