Javascript 需要从谷歌地图API获取纬度和经度
我正在使用从上面链接得到的地图。它在我的网站上运行良好。当我搜索一个地方时,那个位置会显示在地图上。我需要得到它的经度和纬度,当我点击谷歌地图上的某个地方时,它应该显示在一个文本框中 代码如下所示Javascript 需要从谷歌地图API获取纬度和经度,javascript,google-maps,google-maps-api-3,geolocation,Javascript,Google Maps,Google Maps Api 3,Geolocation,我正在使用从上面链接得到的地图。它在我的网站上运行良好。当我搜索一个地方时,那个位置会显示在地图上。我需要得到它的经度和纬度,当我点击谷歌地图上的某个地方时,它应该显示在一个文本框中 代码如下所示 <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
</style>
<title>Places Searchbox</title>
<style>
#target {
width: 345px;
}
</style>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
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)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
// [END region_getplaces]
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAkwr5rmzY9btU08sQlU9N0qfmo8YmE91Y&libraries=places&callback=initAutocomplete"
async defer></script>
</body>
</html>
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}
.控制{
边缘顶部:10px;
边框:1px实心透明;
边界半径:2px 0 0 2px;
框大小:边框框;
-moz框大小:边框框;
高度:32px;
大纲:无;
盒影:0 2px 6px rgba(0,0,0,0.3);
}
#pac输入{
背景色:#fff;
字体系列:Roboto;
字体大小:15px;
字体大小:300;
左边距:12px;
填充:0 11px 0 13px;
文本溢出:省略号;
宽度:300px;
}
#pac输入:焦点{
边框颜色:#4d90fe;
}
.pac集装箱{
字体系列:Roboto;
}
#类型选择器{
颜色:#fff;
背景色:#4d90fe;
填充:5px11px 0px 11px;
}
#类型选择器标签{
字体系列:Roboto;
字体大小:13px;
字体大小:300;
}
放置搜索框
#目标{
宽度:345px;
}
//本例使用Google Place Autocomplete将搜索框添加到地图中
//特色。人们可以进入地理搜索。搜索框将返回一个
//包含位置和预测搜索词组合的拾取列表。
函数initAutocomplete(){
var map=new google.maps.map(document.getElementById('map'){
中心:{lat:-33.8688,lng:151.2195},
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
//创建搜索框并将其链接到UI元素。
var input=document.getElementById('pac-input');
var searchBox=newgoogle.maps.places.searchBox(输入);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(输入);
//将搜索框结果偏向当前地图的视口。
addListener('bounds_changed',function(){
searchBox.setBounds(map.getBounds());
});
var标记=[];
//[开始区域\u getplaces]
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
searchBox.addListener('places\u changed',function(){
var places=searchBox.getPlaces();
如果(places.length==0){
返回;
}
//清除旧的标记。
markers.forEach(函数(marker){
marker.setMap(空);
});
标记=[];
//对于每个位置,获取图标、名称和位置。
var bounds=new google.maps.LatLngBounds();
地点。forEach(功能(地点){
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
//为每个地方创建一个标记。
markers.push(新的google.maps.Marker)({
地图:地图,
图标:图标,
标题:place.name,
位置:place.geometry.location
}));
if(place.geometry.viewport){
//只有地理代码具有视口。
联合(place.geometry.viewport);
}否则{
扩展(place.geometry.location);
}
});
映射边界(bounds);
});
//[结束区域\u getplaces]
}
如果在place collection中已有位置数据,则可以使用loop。下面是最简单的方法
for (var i = 0, place; place = places[i]; i++) {
alert("latitude: "+place.geometry.location.lat()+" longitude: "+place.geometry.location.lng());
});
如果要将其添加到数组中
var locationArray[];
for (var i = 0, place; place = places[i]; i++) {
alert("latitude: "+place.geometry.location.lat()+" longitude: "+place.geometry.location.lng());
locationArray[i]= places[i].geometry.location;
});
下面是代码示例您需要在map对象上放置一个事件侦听器,然后在单击事件发生时获取其坐标:
map.addListener('click', function (locationdata) {
document.getElementById("latitude").value =locationdata.latLng.lat();
document.getElementById("longitude").value =locationdata.latLng.lng();
});
并相应地调整html为每个标记添加一个侦听器,使用它将该标记的位置添加到文本框中
// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
});
google.maps.event.addListener(marker,'click',function(evt) {
document.getElementById('coordinates').value = marker.getPosition().toUrlValue(6);
});
markers.push(marker);
代码片段:
//此示例使用Google Place Autocomplete将搜索框添加到地图中
//特色。人们可以进入地理搜索。搜索框将返回一个
//包含位置和预测搜索词组合的拾取列表。
//单击的标记坐标将放置在id为“坐标”的输入文本元素中
函数initAutocomplete(){
var map=new google.maps.map(document.getElementById('map'){
中心:{
lat:-33.8688,
液化天然气:151.2195
},
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
//创建搜索框并将其链接到UI元素。
var input=document.getElementById('pac-input');
var searchBox=newgoogle.maps.places.searchBox(输入);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(输入);
//将搜索框结果偏向当前地图的视口。
addListener('bounds_changed',function(){
searchBox.setBounds(map.getBounds());
});
var标记=[];
//[开始区域\u getplaces]
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
searchBox.addListener('places\u changed',function(){
var places=searchBox.getPlaces();
如果(places.length==0){
返回;
}
//清除旧的标记。
markers.forEach(函数(marker){
marker.setMap(空);
});
标记=[];
//对于每个位置,获取图标、名称和位置。
var bounds=new google.maps.LatLngBounds();
地点。forEach(功能(地点){
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
//为每个地方创建一个标记。
var marker=new google.maps.marker({
地图:地图,
图标:图标,
标题:place.name,
位置:place.geometry.location
});
google.maps.event.addListener(标记,'click',函数(evt){
document.ge
// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
});
google.maps.event.addListener(marker,'click',function(evt) {
document.getElementById('coordinates').value = marker.getPosition().toUrlValue(6);
});
markers.push(marker);