gpt4 book ai didi

javascript - Google map 嵌入 + 搜索框

转载 作者:行者123 更新时间:2023-11-28 07:42:43 26 4
gpt4 key购买 nike

我正在尝试为我的网站制作一张 map ,它将显示一些餐馆的标记。我还希望人们能够搜索 map ,这样他们就可以查找地址并能够看到该地区的餐馆。我得到了带有我想要的标记的 map 代码:

 <script src='https://maps.googleapis.com/maps/api/js?key=&sensor=false&extension=.js'></script> 

<script>
google.maps.event.addDomListener(window, 'load', init);
var map;
function init() {
var mapOptions = {
center: new google.maps.LatLng(48.142265,11.577987),
zoom: 12,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.DEFAULT,
},
disableDoubleClickZoom: true,
mapTypeControl: false,
scaleControl: false,
scrollwheel: true,
panControl: true,
streetViewControl: false,
draggable : true,
overviewMapControl: false,
overviewMapControlOptions: {
opened: false,
},
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [
{
"featureType": "water",
"stylers": [
{
"visibility": "on"
},
{
"color": "#b5cbe4"
}
]
},
{
"featureType": "landscape",
"stylers": [
{
"color": "#efefef"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#83a5b0"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{
"color": "#bdcdd3"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#e3eed3"
}
]
},
{
"featureType": "administrative",
"stylers": [
{
"visibility": "on"
},
{
"lightness": 33
}
]
},
{
"featureType": "road"
},
{
"featureType": "poi.park",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
},
{
"lightness": 20
}
]
},
{},
{
"featureType": "road",
"stylers": [
{
"lightness": 20
}
]
}
],
}
var mapElement = document.getElementById('restaurantmap');
var map = new google.maps.Map(mapElement, mapOptions);
var locations = [
['blahfl', 'falfjlfs', '9480240', 'undefined', 'www.google.com', 48.1303358, 11.5911791, 'https://mapbuildr.com/assets/img/markers/solid-pin-blue.png'],['fahfah', 'ljlsg', '2742470', 'undefined', 'www.google.com', 48.1367075, 11.5556444, 'https://mapbuildr.com/assets/img/markers/solid-pin-green.png']
];
for (i = 0; i < locations.length; i++) {
if (locations[i][1] =='undefined'){ description ='';} else { description = locations[i][1];}
if (locations[i][2] =='undefined'){ telephone ='';} else { telephone = locations[i][2];}
if (locations[i][3] =='undefined'){ email ='';} else { email = locations[i][3];}
if (locations[i][4] =='undefined'){ web ='';} else { web = locations[i][4];}
if (locations[i][7] =='undefined'){ markericon ='';} else { markericon = locations[i][7];}
marker = new google.maps.Marker({
icon: markericon,
position: new google.maps.LatLng(locations[i][5], locations[i][6]),
map: map,
title: locations[i][0],
desc: description,
tel: telephone,
email: email,
web: web
});
if (web.substring(0, 7) != "http://") {
link = "http://" + web;
} else {
link = web;
}
bindInfoWindow(marker, map, locations[i][0], description, telephone, email, web, link);
}
function bindInfoWindow(marker, map, title, desc, telephone, email, web, link) {
var infoWindowVisible = (function () {
var currentlyVisible = false;
return function (visible) {
if (visible !== undefined) {
currentlyVisible = visible;
}
return currentlyVisible;
};
}());
iw = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
if (infoWindowVisible()) {
iw.close();
infoWindowVisible(false);
} else {
var html= "<div style='color:#000;background-color:#fff;padding:5px;width:150px;'><h4>"+title+"</h4><p>"+desc+"<p><p>"+telephone+"<p><a href='"+link+"'' >"+web+"<a></div>";
iw = new google.maps.InfoWindow({content:html});
iw.open(map,marker);
infoWindowVisible(true);
}
});
google.maps.event.addListener(iw, 'closeclick', function () {
infoWindowVisible(false);
});
}
}
</script>
<style>
#restaurantmap {
height:400px;
width:550px;
}
.gm-style-iw * {
display: block;
width: 100%;
}
.gm-style-iw h4, .gm-style-iw p {
margin: 0;
padding: 0;
}
.gm-style-iw a {
color: #4272db;
}
</style>

<div id='restaurantmap'></div>

我找到了这段代码来添加搜索框(带有自动完成功能,这对我来说很重要): https://google-developers.appspot.com/maps/documentation/javascript/examples/places-searchbox

但我似乎不知道如何将两者混合在一起以使它们一起工作。我的编码知识仍然非常有限:(

我非常感谢您的帮助!

谢谢

最佳答案

包含这样的地点库:

<script src="http://maps.google.com/maps/api/js?libraries=places"></script>

工作代码片段:

google.maps.event.addDomListener(window, 'load', init);
var map;
var markers = [];

function init() {
var mapOptions = {
center: new google.maps.LatLng(48.142265, 11.577987),
zoom: 12,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.DEFAULT,
},
disableDoubleClickZoom: true,
mapTypeControl: false,
scaleControl: false,
scrollwheel: true,
panControl: true,
streetViewControl: false,
draggable: true,
overviewMapControl: false,
overviewMapControlOptions: {
opened: false,
},
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [{
"featureType": "water",
"stylers": [{
"visibility": "on"
}, {
"color": "#b5cbe4"
}]
}, {
"featureType": "landscape",
"stylers": [{
"color": "#efefef"
}]
}, {
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [{
"color": "#83a5b0"
}]
}, {
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [{
"color": "#bdcdd3"
}]
}, {
"featureType": "road.local",
"elementType": "geometry",
"stylers": [{
"color": "#ffffff"
}]
}, {
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [{
"color": "#e3eed3"
}]
}, {
"featureType": "administrative",
"stylers": [{
"visibility": "on"
}, {
"lightness": 33
}]
}, {
"featureType": "road"
}, {
"featureType": "poi.park",
"elementType": "labels",
"stylers": [{
"visibility": "on"
}, {
"lightness": 20
}]
}, {}, {
"featureType": "road",
"stylers": [{
"lightness": 20
}]
}],
}
var mapElement = document.getElementById('restaurantmap');
var map = new google.maps.Map(mapElement, mapOptions);
var locations = [
['blahfl', 'falfjlfs', '9480240', 'undefined', 'www.google.com', 48.1303358, 11.5911791, 'https://mapbuildr.com/assets/img/markers/solid-pin-blue.png'],
['fahfah', 'ljlsg', '2742470', 'undefined', 'www.google.com', 48.1367075, 11.5556444, 'https://mapbuildr.com/assets/img/markers/solid-pin-green.png']
];
for (i = 0; i < locations.length; i++) {
if (locations[i][1] == 'undefined') {
description = '';
} else {
description = locations[i][1];
}
if (locations[i][2] == 'undefined') {
telephone = '';
} else {
telephone = locations[i][2];
}
if (locations[i][3] == 'undefined') {
email = '';
} else {
email = locations[i][3];
}
if (locations[i][4] == 'undefined') {
web = '';
} else {
web = locations[i][4];
}
if (locations[i][7] == 'undefined') {
markericon = '';
} else {
markericon = locations[i][7];
}
marker = new google.maps.Marker({
icon: markericon,
position: new google.maps.LatLng(locations[i][5], locations[i][6]),
map: map,
title: locations[i][0],
desc: description,
tel: telephone,
email: email,
web: web
});
if (web.substring(0, 7) != "http://") {
link = "http://" + web;
} else {
link = web;
}
bindInfoWindow(marker, map, locations[i][0], description, telephone, email, web, link);
}

function bindInfoWindow(marker, map, title, desc, telephone, email, web, link) {
var infoWindowVisible = (function() {
var currentlyVisible = false;
return function(visible) {
if (visible !== undefined) {
currentlyVisible = visible;
}
return currentlyVisible;
};
}());
iw = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
if (infoWindowVisible()) {
iw.close();
infoWindowVisible(false);
} else {
var html = "<div style='color:#000;background-color:#fff;padding:5px;width:150px;'><h4>" + title + "</h4><p>" + desc + "<p><p>" + telephone + "<p><a href='" + link + "'' >" + web + "<a></div>";
iw = new google.maps.InfoWindow({
content: html
});
iw.open(map, marker);
infoWindowVisible(true);
}
});
google.maps.event.addListener(iw, 'closeclick', function() {
infoWindowVisible(false);
});
}

// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));

// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();

if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}

// For each place, get the icon, place name, and location.
markers = [];
var bounds = new google.maps.LatLngBounds();
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)
};

// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});

markers.push(marker);

bounds.extend(place.geometry.location);
}

map.fitBounds(bounds);
});

// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
    #restaurantmap {
height: 400px;
width: 550px;
}
.gm-style-iw * {
display: block;
width: 100%;
}
.gm-style-iw h4,
.gm-style-iw p {
margin: 0;
padding: 0;
}
.gm-style-iw a {
color: #4272db;
}
.controls {
margin-top: 16px;
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;
padding: 0 11px 0 13px;
width: 400px;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
text-overflow: ellipsis;
}

#pac-input:focus {
border-color: #4d90fe;
margin-left: -1px;
padding-left: 14px; /* Regular padding-left + 1. */
width: 401px;
}

.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;
}
<script src="http://maps.google.com/maps/api/js?libraries=places"></script>
<div id='restaurantmap'></div>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">

关于javascript - Google map 嵌入 + 搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826918/

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