【发布时间】:2020-03-06 06:08:30
【问题描述】:
我正在开发一个程序,该程序采用纬度和经度并根据搜索查询生成附近的位置。一切都按预期工作,但我不知道如何使用用户坐标更改起始坐标。我最初的计划是让用户按以下形式输入 lat 和 lng:
<form id="mapCenterForm">
<label for="latitude">lat</label>
<input type="text" id="lat" name="latitude" placeholder="0.000000">
<label for="longitude">lng</label>
<input type="text" id="lng" name="longitude" placeholder="0.000000">
<br>
<input type="submit">
</form>
然后用输入的信息改变脚本中的变量lat,lng:
var map;
var lat= 34.16076;
var lng= -70.20507;
var map=new google.maps.LatLng(lat,lng);
function initMap() {
// Create the map.
var SET = {lat, lng};
map = new google.maps.Map(document.getElementById('map'), {
center: SET,
zoom: 10
});
但我不知道是否有替换地图作为起点加载的 lat 和 lng 的方法。
很抱歉,如果这与发布的其他一些问题相似,我尝试使用Changing latitude and longitude google maps api 寻求指导,但这对我没有多大帮助。
编辑:我正在添加一段完整的 HTML 代码,因为我收到了一个答案,并进行了一些编辑,但它实际上并没有更改 SET 变量。希望人们能够运行它,以便更好地理解这个问题。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Base Mapper</title>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
#right-panel {
font-family: Arial, Helvetica, sans-serif;
position: absolute;
right: 5px;
top: 60%;
margin-top: -395px;
height: 650px;
width: 200px;
padding: 5px;
z-index: 5;
border: 1px solid #999;
background: #fff;
}
h2 {
font-size: 22px;
margin: 0 0 5px 0;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
height: 580px;
width: 200px;
overflow-y: scroll;
}
li {
background-color: #f1f1f1;
padding: 5px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
li:nth-child(odd) {
background-color: #fcfcfc;
}
#more {
width: 100%;
margin: 5px 0 0 0;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
width:70%
}
</style>
<div class="container">
<form id="mapCenterForm">
<label for="latitude">lat</label>
<input type="text" id="lat" name="latitude" placeholder="0.000000">
<label for="longitude">lng</label>
<input type="text" id="lng" name="longitude" placeholder="0.000000">
<br>
<button onclick = "change_center()">
Submit
</button>
</form>
<div id="map" style="height: 500px"></div>
</div>
<script>
var red_icon = 'http://maps.google.com/mapfiles/ms/icons/red-dot.png' ;
var map;
var lat= 41.18076;
var lng= -73.20537;
//THE FOLLOWING LINE WAS REMOVED
//var map=new google.maps.LatLng(lat,lng);
function initMap() {
// Create the map.
var SET = {lat, lng};
map = new google.maps.Map(document.getElementById('map'), {
center: SET,
zoom: 13
});
function change_center() {
var newLat = document.getElementById("lat").value;
var newLng = document.getElementById("lng").value;
var SET = {newLat, newLng};
map.setCenter(new google.maps.LatLng(newLat, newLng));
}
// Create the places service.
var service = new google.maps.places.PlacesService(map);
var getNextPage = null;
var moreButton = document.getElementById('more');
moreButton.onclick = function() {
moreButton.disabled = true;
if (getNextPage) getNextPage();
};
// Perform a nearby search.
service.nearbySearch(
{location: SET, radius: 7500, keyword: "library"},
function(results, status, pagination) {
if (status !== 'OK') return;
createMarkers(results);
moreButton.disabled = !pagination.hasNextPage;
getNextPage = pagination.hasNextPage && function() {
pagination.nextPage();
};
});
}
function createMarkers(places) {
var bounds = new google.maps.LatLngBounds();
var placesList = document.getElementById('places');
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var marker = new google.maps.Marker({
map: map,
icon: red_icon,
title: place.name,
position: place.geometry.location
});
var li = document.createElement('li');
li.textContent = place.name;
placesList.appendChild(li);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
}
</script>
</head>
<body>
<div id="map"></div>
<div id="right-panel">
<h2>Locations</h2>
<ul id="places"></ul>
<button id="more">More Results</button>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=initMap" async defer></script>
</body>
</html>
【问题讨论】:
-
我在发布的代码中收到一个 javascript 错误:
Uncaught ReferenceError: google is not defined在第 110 行:var map=new google.maps.LatLng(lat,lng);(该行在加载 API 脚本之前执行) -
请注意,表单是通过单击按钮提交的,该按钮会重新加载页面。此外,您的
SET值看起来不像google.maps.LatLng或google.maps.LatLngLiteral(尽管它似乎确实有效)。 -
尝试删除行“var map=new google.maps.LatLng(lat,lng);”在 110。我的初始代码在运行时从未有该行,在删除它之后,代码似乎仍然运行良好。此外,lat & lng 变量集仍然有效,因为我刚刚获取了数字集并将它们移动到它们自己的单独变量中以被调用。
-
您的 HTML 也无效,您在文档的
<head>中有表单 -
proof of concept fiddle。更改地图的中心(但不重新运行搜索)。
标签: html api google-maps latitude-longitude