地理位置在页面加载时将值传递给url.action

时间:2013-06-19 09:35:27

标签: javascript jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

任何人都可以帮助解决这个问题,我试图在页面加载时使用地理位置获取纬度经度值,问题是,我在尝试弄清楚如何将值传递给mvc局部视图时遇到问题我现在已经很好地期待它,所以希望一双新眼睛可以看到我的问题。

由于

乔治

<script>
    if (navigator.geolocation) {
        alert("Geo-Enabled");
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    }

    function showPosition(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        @*var actionUrl = '@Html.Raw(Url.Action("CurrentConditions","Weather", new {latitude = lat,longitude = lon}))';*@
        alert(lat + ' ' + lon);
            @*url: '@Url.Action("CurrentConditions","Weather", new {latitude = '+ position.coords.latitude +',longitude = ' + position.coords.longitude+'})',*@
        url: '@Html.Raw(Url.Action("CurrentConditions","Weather", new {latitude = lat,longitude = lon}))'; @*<<<<<<Does not work*@
    }
function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
        alert( "Unable to display accurate weather forcast, location denied.");
      break;
    case error.POSITION_UNAVAILABLE:
        alert( "Location information is unavailable.");
      break;
    case error.TIMEOUT:
        alert( "The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
        alert( "An unknown error occurred.");
      break;
    }
  }
</script>

抱歉走开了,回来整理它,只需休息一下: - )

$.ajax({
            url: '@Html.Raw(Url.Action("CurrentConditions","Weather"))',
            type: 'get',
            alert: (lat),
            data: {
                latitude: lat,
                longitude: lon
            }
        });

1 个答案:

答案 0 :(得分:1)

生成操作URL时JavaScript不会执行。

你可以试试这个

var url = '@Url.Action("CurrentConditions","Weather")';
url = url + "?latitude=" + position.coords.latitude + '&longitude=' + position.coords.longitude;

var url = '@Url.Action("CurrentConditions","Weather", new {latitude = -1,longitude = -2})';
url = url .replace("-1", position.coords.latitude);
url = url .replace("-2", position.coords.longitude);