2017-10-05 13 views
0

実際に私はユーザーの現在の位置を追跡してその動きをトレースしたいと思っています.Asp.Netウェブアプリでも可能ですか?毎秒または連続してそれをトレースする必要があるので。私はlocalhostで緯度と経度を与えるコードをしましたが、IPアドレスではありませんなぜですか?地図上に「何かが間違っている」というエラーが表示されます。 https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocationGeoLocation APIを使用してJavaScriptの現在の位置を取得する

これは、のために正常に動作します:コードは今本体部が

 <body> 
    <label for="latitude">Latitude: </label><input id="latitude" /> <br /> 
    <label for="longitude">Longitude: </label><input id="longitude" /> <br /> 
    <input type="button" onclick="requestPosition()" value="Get Latitude and Longitude" /> 
     <div id="map" style="width:400px;height:400px"> 
      </div> 

     </body> 
+0

これは、JavaScriptコードではなく、C#とASP.NETです。 – krlzlx

+0

@krlzlx私はタグを編集しました。しかし、それをどう扱うか教えてくれますか? – MMG

+0

決して行っていませんが、数行以上のコードが必要ですね! – krlzlx

答えて

0

MozillaのMDNネットワークは、あなたの質問に対する解決策の非常に良い例を持っている ヘッダーセクション

<head runat="server"> 
<title></title> 
     <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor"></script> 
    <script type="text/javascript"> 
    function setText(val, e) { 
    document.getElementById(e).value = val; 
     } 
    function insertText(val, e) { 
document.getElementById(e).value += val; 
    } 

    var nav = null; 
function requestPosition() { 
    if (nav == null) { 
    nav = window.navigator; 
    } 
    if (nav != null) { 
    var geoloc = nav.geolocation; 
    if (geoloc != null) { 
     geoloc.getCurrentPosition(successCallback); 
    } 
    else { 
     alert("geolocation not supported"); 
    } 
} 
else { 
    alert("Navigator not found"); 
    } 
    } 
function successCallback(position) 
    { 
    setText(position.coords.latitude, "latitude"); 
    setText(position.coords.longitude, "longitude"); 
    var latlng = new google.maps.LatLng(position.coords.latitude, 
    position.coords.latitude); 
    var myOptions = { 
    zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map"), myOptions); 
    var marker = new google.maps.Marker 
    (
    { 
    position: new google.maps.LatLng(position.coords.latitude, 
    position.coords.longitude), 
    map: map, 
    title: 'Click me' 
    } 
    ); 
    } 
    </script> 
    </head> 

以下に示します。私はJSFiddle(https://jsfiddle.net/api/mdn/)にありますが、下記のStackOverflowコードスニペット(少なくともChromeの最新バージョンでは)ではありません。これは、GeoLocation APIに関するクロスオリジンポリシーの問題である可能性があります。

function geoFindMe() { 
 
    var output = document.getElementById("out"); 
 

 
    if (!navigator.geolocation){ 
 
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>"; 
 
    return; 
 
    } 
 

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

 
    output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>'; 
 

 
    var img = new Image(); 
 
    img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=true"; 
 

 
    output.appendChild(img); 
 
    } 
 

 
    function error() { 
 
    output.innerHTML = "Unable to retrieve your location"; 
 
    } 
 

 
    output.innerHTML = "<p>Locating…</p>"; 
 

 
    navigator.geolocation.getCurrentPosition(success, error); 
 
}
body { 
 
    padding: 20px; 
 
    background-color:#ffffc9 
 
} 
 

 
p { margin : 0; }
<p><button onclick="geoFindMe()">Show my location</button></p> 
 
<div id="out"></div>

+0

私のコードは、GoogleApiキーを追加した後もlocalhost上でうまく動作しますが、ドメイン(ライブ)に移動すると動作しません。 – MMG

+0

この質問は非常によく似ています:https://stackoverflow.com/a/19740416/ 2147110 – dperish

関連する問題