开发者

Check browser geo location capability and get Latitude and Longtitude

I am developing .net MVC application.

I want to get Geo Location from if it is support html 5.0 browser.

But I want to know can I do it inside a Cont开发者_如何学Croller (HomeController, inside index method)

Need your suggestions

Thank You Yohan


If you add a reference to jQuery in your project, you should then be able to add the following Javascript code to your view. This will then post to your controller with the long and lat. That is the only way you will be able to get the GeoLocation in your controller.

NOTE: This code hasn't been tested! I just wrote it quickly from memory.

// 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) {
  });
}

You will also need to add the parameters "latitude" and "longtitude" to your controller.

  public ActionResult Index(string latitude, string longtitude)
  {

  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜