1

谁能帮忙解决这个问题,我试图在页面加载时使用地理位置获取纬度经度值,问题是,我在试图弄清楚如何将值传递给 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
            }
        });
4

1 回答 1

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);
于 2013-06-19T09:42:47.290 回答