2013-03-25 72 views
1

我是mvc和jquery的新手。 我想通過將它們替換爲數據庫上的多個城市來替換它們。我想知道我是如何做到這一點的。mvc4:將位置信息傳遞給javascript變量以獲取地理位置

function initialize() { 

    var locations = [ 
     ['Hougang', 1.37265, 103.893658], 
     ['Punggol', 1.400617, 103.907833], 
     ['MacRitchie Reservoir', 1.346002, 103.825436], 
     ['Bishan', 1.352051, 103.849125], 
     ['Sentosa', 1.251226, 103.830757] 
     ]; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 0, 
     center: new google.maps.LatLng(1.37265, 103.893658), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
      map: map 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
      return function() { 
       infowindow.setContent(locations[i][0]); 
       infowindow.open(map, marker); 
      } 
     })(marker, i)); 
    } 

    // Check if user support geo-location 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      var latitude = position.coords.latitude; 
      var longitude = position.coords.longitude; 
      var geolocpoint = new google.maps.LatLng(latitude, longitude); 

      var mapOptions = { 
       zoom: 8, 
       center: geolocpoint, 
       mapTypeId: google.maps.MapTypeId.HYBRID 
      } 
      // Place a marker 
      var geolocation = new google.maps.Marker({ 
       position: geolocpoint, 
       map: map, 
       title: 'Your geolocation', 
       icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png' 
      }); 
     }); 
    } 
} 
google.maps.event.addDomListener(window, 'load', initialize); 


</script> 

這是關於這些行嗎?,問題是我不知道如何連接兩個。

public JsonResult Autolocations() 
     { 
      var locations = from s in db.Tble_content 
           select s.EN_Title; 
      // return namelist.ToList(); 
      return Json(locations, JsonRequestBehavior.AllowGet); 
     } 

提前感謝 碎甲彈

回答

1

你應該使用AJAX來控制,以期連接,像這樣

$(document).ready(function() { 

    $.getJSON("/Locations/Autolocations", null, function (locations) { 
      initialize(locations); 
    }); 
}); 

function initialize(locations) { 
    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 0, 
     center: new google.maps.LatLng(1.37265, 103.893658), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    //the rest of your javascript code 
} 

而且你的控制器應是這樣的

public class LocationsController : Controller  
{ 
    public JsonResult Autolocations() 
    { 
     var locations = from s in db.Tble_content 
          select s; 
     return Json(locations.ToList(), JsonRequestBehavior.AllowGet); 
    } 
} 
+0

嗨DZL 對不起,我沒有早點回來,這是gre在我嘗試它,但我得到一個錯誤在JavaScript msg框說[對象對象],[對象對象] ...你知道爲什麼嗎? 但再次感謝這 – hesh 2013-03-26 09:47:12

+0

我認爲你的問題是使用位置[我] [1]。我會使用locations [i] .lat替代索引,並且您的控制器操作需要返回包含屬性lat的位置對象。你能發佈拋出錯誤的javascript行嗎? – 2013-03-26 10:25:00

+0

嗨對不起DZL - 我確實通過添加sql字段名稱來解決問題 position:new google.maps.LatLng(locations [i] [「slatitude」],locations [i] [「slongitude」]),...這已經完成了這項工作。非常感謝您的幫助 Hesh – hesh 2013-03-27 11:01:07