检查浏览器地理位置功能并获取纬度和经度

yoh*_*hna 1 api asp.net-mvc html5 geolocation visual-studio-2010

我正在开发.net MVC应用程序.

如果它支持html 5.0浏览器,我想获得Geo Location.

但我想知道我可以在Controller内部(HomeController,内部索引方法)

需要你的建议

谢谢Yohan

Dea*_*ano 6

如果在项目中添加对jQuery的引用,则应该能够将以下Javascript代码添加到视图中.然后,这将使用long和lat发送到您的控制器.这是您能够在控制器中获得GeoLocation的唯一方法.

注意:此代码尚未经过测试!我只是从记忆中快速写出来.

// Get the coordinates
navigator.geolocation.getCurrentPosition(show_map);

function show_map(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

  // post to your controller
  var url = "/Home/Index?latitude=" + latitude + "&longtitude=" + longitude; 
  $.post(url, function(data) {
  });
}
Run Code Online (Sandbox Code Playgroud)

您还需要将参数"latitude"和"longtitude"添加到控制器.

  public ActionResult Index(string latitude, string longtitude)
  {

  }
Run Code Online (Sandbox Code Playgroud)