2012-10-08 26 views
0

嗨我想填充谷歌地圖上的標記和使用Json從數據庫中讀取經度和緯度。谷歌地圖api v3 + json不工作+ asp.netnet mvc3 c#

作爲第一步,我試圖做的是看我是否得到任何迴應。代碼如下:

實體如下:

public class Property 
{ 
    public int PropertyId { get; set; } 
    ... 
    public virtual PropertyAddress PropertyAddress { get; set; } 
} 

public class PropertyAddress 
{ 
    [Key] 
    public int PropertyId { get; set; } 
    ... 
    public float Latitude { get; set; } 
    public float Longitude { get; set; } 
} 

的Json物業型號:

public class JsonProperty 
{ 
    public int PropertyId { get; set; } 
    public float Latitude { get; set; } 
    public float Longitude { get; set; } 
} 

控制器:

public ActionResult Map() 
    { 
     if (Request.IsAjaxRequest()) 
     { 
      var properties = websiteRepository.FindAllProperties(); 

      var jsonProperties = from property in properties 
           select new JsonProperty 
           { 
            PropertyId = property.PropertyId, 
            Latitude = property.PropertyAddress.Latitude, 
            Longitude = property.PropertyAddress.Longitude 
           }; 

      return Json(jsonProperties.ToList()); 
     } 
     else 
     { 
      return View(); 
     } 
    } 

查看:

@section Scripts_footer { 
    <!-- Google Map Script --> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      var map = new google.maps.Map(document.getElementById("map"), { 
       center: new google.maps.LatLng(19.0759837, 72.8776559), 
       zoom: 13, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }); 

      $.getJSON("/Search/Map", function (json) { 
       alert(json); 
      }); 
     }); 
    </script> 
} 

<div id="map" style="width: 500px; height: 300px"></div> 

我期待與對象對象對象的警報消息......不知道爲什麼沒有發生。

+0

您可以縮短您的問題**如何調試getJSON **或嘗試先做一些[research](http://stackoverflow.com/search?q=debug+getjson)。 –

+0

我會,但我不知道是什麼問題..我真的是新的網絡開發..道歉 – Tripping

+0

首先,我會檢查錯誤的Chrome控制檯..任何? –

回答

2

變化return語句在控制器代碼:

return Json(jsonProperties.ToList(), JsonRequestBehavior.AllowGet); 

Json的請求隱含阻止出於安全原因。

+0

完美謝謝..我從中學到的教程並不需要它。他們也在進行獲取操作。 – Tripping

+0

我不知道有多少人犯過同樣的錯誤,這很常見。 – Tx3