gpt4 book ai didi

javascript - 如何获取和设置谷歌地图的地理位置?

转载 作者:行者123 更新时间:2023-11-28 07:58:30 24 4
gpt4 key购买 nike

目标:

我想创建一个简单的网站,根据您的地址(通过您的 IP 找到)或输入的地理编码显示您的悬浮情况。

我被困在哪里:

我正在使用 http://ipinfo.io/通过 IP 跟踪用户的地理位置。它将地理位置作为 response.loc 返回(如以下代码所示)。

然后我想将该地理位置添加到谷歌地图并将其作为默认值。

var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(59.6162,16.5528);
var mapOptions = {
zoom: 13,
center: latlng,
mapTypeId: 'hybrid'
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(15);
marker = new google.maps.Marker({
position: latlng,
map: map

});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}

google.maps.event.addDomListener(window, 'load', initialize);

// get geocode from IP //

$.get("http://ipinfo.io", function (response) {
$("#ip").html("Your IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html("Location: " + response.loc);
}, "jsonp");
      html, body {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: relative;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
width: 350px;
}
#latlng {
width: 225px;
}
#map-canvas {
height: 50%;
width: 50%;
margin-left: 25%;
margin-top: 5%;
}

.cont {
width: 100%;
max-height: 500px;
position: relative;
overflow: hidden;
background: #f8f8f8;
z-index:-10;
padding-bottom: 5%;
}

h3 {
color: black;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Find out your elevation</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
</head>
<body>

<div class="cont">

<div><h3>Find out what your elevation is!</h3></div>
<hr>
<div id="ip"></div>
<div id="address"></div>
<hr/>Geo location: <pre id="details"></pre>




</div>
<p align="center">Try a different geolocation:</p>
<div id="panel">
<input id="latlng" type="text" value="59.3323050,18.0528360">
<input type="button" value="use geocode" onclick="codeLatLng()">
</div>

<div id="map-canvas"></div>

</body>
</html>

最佳答案

您使用 http://ipinfo.io , 但我发现这个 google geolocation功能,根据纬度和经度找到用户的位置,然后将位置传递给您的 map !

navigator.geolocation.getCurrentPosition(function(position) {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude); // get the position based on the latitude and the longitude

var infowindow = new google.maps.InfoWindow({
map: map,
position: pos, // set the position of the google map
content: 'Location found using HTML5.'
});

map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}

关于javascript - 如何获取和设置谷歌地图的地理位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29960206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com