2013-03-25 70 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