Javascript 谷歌地图API v3-从搜索框中将谷歌地图圈放置为Lat/Lng
正如我的代码所示,这是一个相当新的概念。我的代码主要取自Google开发者页面,用于使用Google Maps API v3 我试图做的是使用PHP将MySql数据库中的一定数量的位置加载到GoogleMaps中。然后,我希望能够使用搜索框的结果lat/lng在地图上放置一个可编辑的圆,以便用户可以更改半径并移动圆,并查看圆覆盖的位置。如果用户进行新的搜索,我希望在搜索框中的新lat/lng处移除并再次替换圆圈 到目前为止,我已经做到:Javascript 谷歌地图API v3-从搜索框中将谷歌地图圈放置为Lat/Lng,javascript,google-maps-api-3,geometry,search-box,Javascript,Google Maps Api 3,Geometry,Search Box,正如我的代码所示,这是一个相当新的概念。我的代码主要取自Google开发者页面,用于使用Google Maps API v3 我试图做的是使用PHP将MySql数据库中的一定数量的位置加载到GoogleMaps中。然后,我希望能够使用搜索框的结果lat/lng在地图上放置一个可编辑的圆,以便用户可以更改半径并移动圆,并查看圆覆盖的位置。如果用户进行新的搜索,我希望在搜索框中的新lat/lng处移除并再次替换圆圈 到目前为止,我已经做到: 使用PHP将位置从MySql加载到Google地图 插入搜
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Places Searchbox</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#description {
font-family: Roboto;
font-size: 15px;
font-weight: 300;
}
#infowindow-content .title {
font-weight: bold;
}
#infowindow-content {
display: none;
}
#map #infowindow-content {
display: inline;
}
.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}
#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls {
display: inline-block;
padding: 5px 11px;
}
.pac-controls label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
#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: 400px;
}
#pac-input:focus {
border-color: #4d90fe;
}
#title {
color: #fff;
background-color: #4d90fe;
font-size: 25px;
font-weight: 500;
padding: 6px 12px;
}
#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.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13,
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 = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
//ADDING CIRCLE HERE - 21.01.20178 19:25PM GMT
var circle = new google.maps.Circle ({
map: map,
center: new google.maps.LatLng (-33.856717, 151.215288),
radius: 1000,
strokeColor: "#00ff00",
fillcolor: "red",
editable: true
});
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) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
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);
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=&libraries=places&callback=initAutocomplete"
async defer></script>
</body>
</html>
放置搜索框
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#描述{
字体系列:Roboto;
字体大小:15px;
字体大小:300;
}
#infowindowcontent.title{
字体大小:粗体;
}
#信息窗口内容{
显示:无;
}
#地图#信息窗口内容{
显示:内联;
}
.pac卡{
利润率:10px 10px 0;
边界半径:2px 0 0 2px;
框大小:边框框;
-moz框大小:边框框;
大纲:无;
盒影:0 2px 6px rgba(0,0,0,0.3);
背景色:#fff;
字体系列:Roboto;
}
#pac容器{
垫底:12px;
右边距:12px;
}
.pac控制{
显示:内联块;
填充:5px11px;
}
.pac控制标签{
字体系列:Roboto;
字体大小:13px;
字体大小:300;
}
#pac输入{
背景色:#fff;
字体系列:Roboto;
字体大小:15px;
字体大小:300;
左边距:12px;
填充:0 11px 0 13px;
文本溢出:省略号;
宽度:400px;
}
#pac输入:焦点{
边框颜色:#4d90fe;
}
#头衔{
颜色:#fff;
背景色:#4d90fe;
字体大小:25px;
字号:500;
填充:6px 12px;
}
#目标{
宽度:345px;
}
//本例使用Google Place Autocomplete将搜索框添加到地图中
//特色。人们可以进入地理搜索。搜索框将返回一个
//包含位置和预测搜索词组合的拾取列表。
//此示例需要Places库。包括图书馆=地方
//第一次加载API时的参数。例如:
//
函数initAutocomplete(){
var map=new google.maps.map(document.getElementById('map'){
中心:{lat:-33.8688,lng:151.2195},
缩放:13,
mapTypeId:“路线图”
});
//创建搜索框并将其链接到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标记=[];
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
//在此处添加圆圈-21.01.20178格林威治标准时间19:25
var circle=new google.maps.circle({
地图:地图,
中心:新google.maps.LatLng(-33.856717151.215288),
半径:1000,
strokeColor:#00ff00“,
填充颜色:“红色”,
可编辑:真
});
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(功能(地点){
如果(!place.geometry){
log(“返回的位置不包含几何图形”);
返回;
}
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
if (index == 0) {
circle.setCenter(place.geometry.location);
}