Javascript 如何在谷歌地图API上定位自定义按钮控件?
我在谷歌地图APi v3上构建了这个简单的地图Javascript 如何在谷歌地图API上定位自定义按钮控件?,javascript,html,css,google-maps,google-maps-api-3,Javascript,Html,Css,Google Maps,Google Maps Api 3,我在谷歌地图APi v3上构建了这个简单的地图 <html> <head> <title>Simple Map</title> <meta name="viewport" content="initial-scale=1.0"> <meta charset="utf-8"> <style> /* Always set the map height explicitl
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<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;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
var myOptions = {
zoom: 8,
center: {lat: -34.397, lng: 150.644},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
//### Add a button on Google Maps ...
var controlMarkerUI = document.createElement('DIV');
controlMarkerUI.style.cursor = 'pointer';
controlMarkerUI.style.backgroundImage = "url(http://localhost/marker.png)";
controlMarkerUI.style.height = '28px';
controlMarkerUI.style.width = '25px';
controlMarkerUI.style.top = '11px';
controlMarkerUI.style.left = '120px';
controlMarkerUI.title = 'Click to set the map to Home';
//myLocationControlDiv.appendChild(controlUI);
map.controls[google.maps.ControlPosition.LEFT_TOP].push(controlMarkerUI);
//### Add a button on Google Maps ...
var controlTrashUI = document.createElement('DIV');
controlTrashUI.style.cursor = 'pointer';
controlTrashUI.style.backgroundImage = "url(http://localhost/trash.png)";
controlTrashUI.style.height = '28px';
controlTrashUI.style.width = '25px';
controlTrashUI.style.top = '11px';
controlTrashUI.style.left = '150px';
controlTrashUI.title = 'Click to set the map to Home';
//myLocationControlDiv.appendChild(controlUI);
map.controls[google.maps.ControlPosition.LEFT_TOP].push(controlTrashUI);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=<PUT_YOUR_API-HERE>&callback=initMap"
async defer></script>
</body>
</html>
简单地图
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
var映射;
函数initMap(){
变量myOptions={
缩放:8,
中心:{纬度:-34.397,液化天然气:150.644},
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map”),myOptions);
//###在谷歌地图上添加一个按钮。。。
var controlMarkerUI=document.createElement('DIV');
controlMarkerUI.style.cursor='指针';
controlMarkerUI.style.backgroundImage=“url(http://localhost/marker.png)";
controlMarkerUI.style.height='28px';
controlMarkerUI.style.width='25px';
controlMarkerUI.style.top='11px';
controlMarkerUI.style.left='120px';
controlMarkerUI.title='单击以将地图设置为主页';
//myLocationControlDiv.appendChild(controlUI);
控件[google.maps.ControlPosition.LEFT_TOP].push(controlMarkerUI);
//###在谷歌地图上添加一个按钮。。。
var controltrahui=document.createElement('DIV');
controlTrashUI.style.cursor='pointer';
controlTrashUI.style.backgroundImage=“url(http://localhost/trash.png)";
controlTrashUI.style.height='28px';
controlTrashUI.style.width='25px';
controlTrashUI.style.top='11px';
controlTrashUI.style.left='150px';
controlTrashUI.title='单击以将地图设置为主页';
//myLocationControlDiv.appendChild(controlUI);
map.controls[google.maps.ControlPosition.LEFT_TOP].push(controltrashu);
}
映射结果如下所示
在我的代码中,marker和trash这两个按钮的样式是以这样一种方式设置的,应该放在“卫星”按钮旁边
为什么不保留我的样式设置
建议/示例?我有一个答案,如果这是您正在寻找的,则答案不是100%,但这是修改后的代码
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<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;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
var myOptions = {
zoom: 8,
center: {lat: -34.397, lng: 150.644},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
// Create a div to hold the control.
var controlDiv = document.createElement('div');
//### Add a button on Google Maps ...
var controlMarkerUI = document.createElement('div');
controlMarkerUI.style.cursor = 'pointer';
controlMarkerUI.style.backgroundColor = 'blue';
controlMarkerUI.style.height = '28px';
controlMarkerUI.style.width = '25px';
controlMarkerUI.style.marginLeft = '10px';
controlMarkerUI.style.marginTop = '10px';
controlMarkerUI.title = 'Click to set the map to Home';
controlDiv.appendChild(controlMarkerUI);
//### Add a button on Google Maps ...
var controlTrashUI = document.createElement('div');
controlTrashUI.style.cursor = 'pointer';
controlTrashUI.style.backgroundColor = 'black';
controlTrashUI.style.height = '28px';
controlTrashUI.style.width = '25px';
controlTrashUI.style.marginLeft = '60px';
controlTrashUI.title = 'Click to set the map to Home';
controlMarkerUI.appendChild(controlTrashUI);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(controlDiv);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=<PUT KEY
HEREEEE>&callback=initMap"
async defer></script>
</body>
</html>
简单地图
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
var映射;
函数initMap(){
变量myOptions={
缩放:8,
中心:{纬度:-34.397,液化天然气:150.644},
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map”),myOptions);
//创建一个div来保存控件。
var controlDiv=document.createElement('div');
//###在谷歌地图上添加一个按钮。。。
var controlMarkerUI=document.createElement('div');
controlMarkerUI.style.cursor='指针';
controlMarkerUI.style.backgroundColor='蓝色';
controlMarkerUI.style.height='28px';
controlMarkerUI.style.width='25px';
controlMarkerUI.style.marginLeft='10px';
controlMarkerUI.style.marginTop='10px';
controlMarkerUI.title='单击以将地图设置为主页';
controlDiv.appendChild(controlMarkerUI);
//###在谷歌地图上添加一个按钮。。。
var controltrahui=document.createElement('div');
controlTrashUI.style.cursor='pointer';
controlTrashUI.style.backgroundColor='black';
controlTrashUI.style.height='28px';
controlTrashUI.style.width='25px';
controlTrashUI.style.marginLeft='60px';
controlTrashUI.title='单击以将地图设置为主页';
controlMarkerUI.appendChild(ControlTrahui);
map.controls[google.maps.ControlPosition.TOP\u LEFT].push(controlDiv);
}
你必须把你的徽标改回你的本地主机我想
是做你想做的事情的最好方式。别忘了你的API密钥
输出: