2017-05-21 7 views
-2

私は緯度と経度を表示するユーザーの場所を検出し、それらに対して物理アドレスを表示するアプリケーションで作業しています。私がこれを実行するとき、私は何も取得しません。C#現在の緯度と経度をasp.netアプリケーションで取得する

私のシステムの位置は有効で、インターネットに接続されています。私はいつも誰かが場所を追跡しているタスクバーの点を見るが、まだ何も得ていない。

ここに私のコードです。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Device.Location; 
using System.Diagnostics; 
namespace APIsProject 
{ 
public partial class GetLatitudeandLongitude : System.Web.UI.Page 
{ 
    private string latitude; 
    private string longitute; 
    private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(); 
     watcher = new GeoCoordinateWatcher(); 
     // Catch the StatusChanged event. 
     watcher.StatusChanged += Watcher_StatusChanged; 
     // Start the watcher. 
     watcher.Start(); 

    } 

    private void Watcher_StatusChanged(object sender, 
    GeoPositionStatusChangedEventArgs e) // Find GeoLocation of Device 
    { 
     try 
     { 
      if (e.Status == GeoPositionStatus.Ready) 
      { 
       // Display the latitude and longitude. 
       if (watcher.Position.Location.IsUnknown) 
       { 
        latitude = "0"; 
        longitute = "0"; 
       } 
       else 
       { 
        latitude = 
     watcher.Position.Location.Latitude.ToString(); 
        longitute = 
     watcher.Position.Location.Longitude.ToString(); 
       } 
      } 
      else 
      { 
       latitude = "0"; 
       longitute = "0"; 
      } 
     } 
     catch (Exception) 
     { 
      latitude = "0"; 
      longitute = "0"; 
     } 
    } 

    protected void InsertButton_Click(object sender, EventArgs e) 
    { 
     TextBoxLatitude.Text = latitude; 
     TextBoxLongitude.Text = longitute; 

    } 
} 
} 
+1

あなたはASP.NETアプリケーションで、あなたは常に**サーバーの場所を追跡することを知っています**?訪問者の位置をトラッキングするには、クライアントサイドのサービスを使用する必要があります。 – Filburt

+0

平均私はページに埋め込まれたJavaScriptを使用する必要がありますか? –

+0

はい。それ以外の方法は? – Mardoxx

答えて

4

あなたはnavigator.geolocation.getCurrentPosition()を使用して位置を見つけるために、これを試すことができます。

<html> 
<body> 

<p>Click the button to get your coordinates.</p> 

<button onclick="getLocation()">Try It</button> 

<p id="demo"></p> 

<script> 
var x = document.getElementById("demo"); 

function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 

function showPosition(position) { 
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
} 
</script> 

</body> 
</html> 

Live demo

関連する問題