【问题标题】:How to get Geo location from the WebURL如何从 WebURL 获取地理位置
【发布时间】:2016-06-21 23:38:00
【问题描述】:
  • 我创建了一个 ASP.NET 网站并将其托管在服务器上。
  • 假设我的 Web 访问 URL 是 "www.xyz.com"
  • 现在我想要Geo Location 访问我的网站并将其显示在 Google 地图上。或者我可以从访问此 Web-URL 的位置获取 latitudelongitude 吗?

那么,这可能吗?

【问题讨论】:

  • 首先从网站访问的地方获取用户的位置。
  • 如何获取用户的位置?

标签: javascript asp.net google-maps geolocation latitude-longitude


【解决方案1】:

使用HTML Geolocation。 HTML Geolocation API 用于定位用户的位置。你可以得到纬度&&经度! 这是我的代码:

if (navigator.geolocation) {
    // support geolocation
    var success = function(position){
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
        console.log('here latitude :'+latitude+' && longitude :'+longitude);
    }
    var errors = function(){
        console.log('not working');
    }
    navigator.geolocation.getCurrentPosition(success, errors);
} else {
    alert('upgrade your browser !');
}

我的输出是:这里纬度:37.5030042 && 经度:127.0507571

编辑

您想查看访问您网站的所有用户地理位置。 我认为有两种方式。使用谷歌分析 api 和谷歌地图或使用 ajax(5sec 将新的地理位置数据更新到 html 和你的数据库(mysql、dynamo、redis 等))

 if (navigator.geolocation) {
         // support geolocation
         var success = function(position){
             latitude = position.coords.latitude;
             longitude = position.coords.longitude;
             console.log('here latitude :'+latitude+' && longitude :'+longitude);

             setInterval(function(e){
                 $.ajax({
                   type: "get",
                   dataType: 'json',
                   url: 'here request url',
                   cache: false,
                   data: { // request user geo data
                       latitude : latitude,
                       longitude : longitude
                   }
                 }).done(function(xhr) {
                     if(xhr.status){
                         // response users geo data and update your html DOM
                         $('#maps').html(xhr.geo.datas)
                     }else{
                         alert(xhr.msg);
                     }
                 })
             },5000); // default 5 second
         }
         var errors = function(){
             console.log('not working');
         }
         navigator.geolocation.getCurrentPosition(success, errors);

     } else {
         alert('upgrade your browser !');
     }

【讨论】:

  • 这适用于单个用户。我想查看访问我网站的所有用户地理位置
  • 使用谷歌分析API和谷歌地图或使用ajax(5s更新新的地理数据)
  • @Chetan Sanghani 请查看此文档here
【解决方案2】:

这是 HTML 代码

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
        <asp:BoundField DataField="CountryName" HeaderText="Country" />    
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Latitude" />
    </Columns>
</asp:GridView>

这是 C# 代码:

protected void Page_Load(object sender, EventArgs e)
{
    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ipAddress))
    {
        ipAddress = Request.ServerVariables["REMOTE_ADDR"];
    }

    string APIKey = "<Your API Key>";
    string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
    using (WebClient client = new WebClient())
    {
        string json = client.DownloadString(url);
        Location location = new JavaScriptSerializer().Deserialize<Location>(json);
        List<Location> locations = new List<Location>();
        locations.Add(location);
        gridView1.DataSource = locations;
        gridView1.DataBind();
    }
}


public class Location
{
    public string IPAddress { get; set; }
    public string CountryName { get; set; }      
    public string Latitude { get; set; }
    public string Longitude { get; set; }       
}

【讨论】:

    【解决方案3】:

    尝试这样获取用户的位置: 除非您设置超时和错误,否则您获取当前位置的请求可能永远不会返回。

    window.onload = function() {
      var startPos;
      var geoOptions = {
      timeout: 10 * 1000
    }
    
    var geoSuccess = function(position) {
      startPos = position;
      document.getElementById('startLat').innerHTML = startPos.coords.latitude;
      document.getElementById('startLon').innerHTML = startPos.coords.longitude;
    };
    var geoError = function(error) {
      console.log('Error occurred. Error code: ' + error.code);
      // error.code can be:
      //   0: unknown error
      //   1: permission denied
      //   2: position unavailable (error response from location provider)
      //   3: timed out
     };
    
       navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
     };
    

    【讨论】:

    • 这适用于单个用户。我想查看访问我网站的所有用户地理位置
    • 我想同时显示所有用户的地理位置,
    • 试图存储在数组中但无法测试。
    • 那么,我怎样才能得到完美的结果?
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多