gpt4 book ai didi

javascript - Google Maps API v3 - 将 Google Maps Circle 作为搜索框中的 Lat/Lng

转载 作者:行者123 更新时间:2023-11-30 14:49:33 25 4
gpt4 key购买 nike

正如我的代码所显示的那样,这是相当新的。我所拥有的代码主要取自用于使用 Google Maps API v3 的 Google Developers 页面。

我想做的是使用 PHP 将一定数量的位置从 MySql 数据库加载到 Google map 中。然后我希望能够使用搜索框的结果 lat/lng 在 map 上放置一个可编辑的圆圈,以便用户可以更改半径并移动圆圈并查看圆圈覆盖的位置。如果用户进行新的搜索,我打算将圆圈从搜索框中删除并重新替换为新的经纬度。

到目前为止我已经做到了:

  1. 使用 PHP 将位置从 MySql 加载到 Google map
  2. 插入一个可平移到找到的地址的搜索框
  3. 在设定的纬度/经度处输入一个可编辑的圆。这就是我遇到的问题,因为我希望动态放置可编辑的圆圈。

任何帮助将不胜感激。谢谢。

21.01.2018 19:35 GMT:以下代码的新示例取自 Google map 开发人员页面,因为发布的旧版本代码具有指向 MySQL 数据库的 Php 链接,其他任何人都无法加载。这是来自 Google Developers 页面 (https://developers.google.com/maps/documentation/javascript/examples/places-searchbox) 的搜索框示例代码,我添加了一些非常基本的代码来添加一个可编辑的圈子。

同样的想法是,当用户搜索位置而不是将标记放置在 map 上时,将放置一个可编辑的圆圈。每次搜索新位置时,都会从 map 上清除可编辑的圆圈。我会有一个输入框,用户可以在其中输入圆的半径(以米为单位)。在框中输入半径后,圆的半径会发生变化以匹配它。

新代码:

 <!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>

最佳答案

要将圆移到 SearchBox 中的第一个结果,请将圆的中心设置为结果的位置。将此添加到 place_changed 事件处理中:

if (index == 0) {
circle.setCenter(place.geometry.location);
}

proof of concept fiddle

代码片段:

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 = [];

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, index) {
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
}));

// center the circle on the first result
if (index == 0) {
circle.setCenter(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);
});
}
google.maps.event.addDomListener(window, "load", initAutocomplete);
/* 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;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>

关于javascript - Google Maps API v3 - 将 Google Maps Circle 作为搜索框中的 Lat/Lng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48366199/

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