[英]Generate random latitude and longitude with javascript after 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.