簡體   English   中英

ASP.NET中的Javascript Geolocation緯度和經度變量訪問

[英]Javascript Geolocation latitude and longitude variable access in asp.net

我正在嘗試使用javascript使用地理位置來獲取用戶位置,我可以看到當前位置以及所有位置。 但是,當我試圖獲取坐標變量到c#對象時,它會返回任何東西。 以下是我的JavaScript。

  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=ABC"></script> <script type="text/javascript"> if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function (p) { var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude); var mapOptions = { center: LatLng, zoom: 20, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); var marker = new google.maps.Marker({ position: LatLng, map: map, title: "<div style = 'height:60px;width:200px'><b>Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude, }); google.maps.event.addListener(marker, "click", function (e) { var infoWindow = new google.maps.InfoWindow(); infoWindow.setContent(marker.title); infoWindow.open(map, marker); // document.getElementById("latitude").value = p.coords.latitude; // document.getElementById("longitude").value = p.coords.latitude; }); var longitude = document.getElementById('latitude'), latitude = document.getElementById('longitude'); function GetPosition(p) { var long = p.coords.longitude, lat = p.coords.latitude longitude.text(long); latitude.text(lat); } }); } else { alert('Location feature is not supported in this browser.'); } </script> 

和身體

  <div> <div id="dvMap" style="width: 500px; height: 500px"></div> <asp:Label ID="latitude" runat="server"></asp:Label> <asp:Label ID="longitude" runat="server"></asp:Label> </div> 

還嘗試了與GeoCoordinateWatcher一起失敗。 我可以獲取用戶位置的任何其他功能。 提前致謝。

@NAJEEB,請嘗試以下解決方案

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=ABC&callback=myMap"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      var longitude = $('#longitude');
       var latitude = $('#latitude');
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (p) {
             console.log(longitude);
                    longitude.text(p.coords.longitude);
                    latitude.text(p.coords.latitude);
                var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);

                var mapOptions = {
                    center: LatLng,
                    zoom: 20,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
                var marker = new google.maps.Marker({
                    position: LatLng,
                    map: map,
                    title: "<div style = 'height:60px;width:200px'><b>Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude,

                });
                google.maps.event.addListener(marker, "click", function (e)                 {
                    var infoWindow = new google.maps.InfoWindow();
                    infoWindow.setContent(marker.title);
                    infoWindow.open(map, marker);
                });
            });
        } else {
            alert('Location feature is not supported in this browser.');
        }
        });
    </script>
<title>Page Title</title>
</head>
<body>
 <div>
  <label ID="latitude"></label>
 <label ID="longitude"></label>
 <br/>
 <div id="dvMap" style="width: 500px; height: 500px"></div>
 </div>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM