简体   繁体   English

如何在Google Map API V3上添加切换按钮?

[英]How to add toggle button on google map api v3?

I found library: geometrycontrols that is written to api v2 and allows adding buttons. 我找到了库: geometrycontrols ,它被写入api v2,并允许添加按钮。 How to make a toggle button to add a marker in the api v3? 如何制作切换按钮以在api v3中添加标记? I have initiated map etc. 我已经启动地图等。

You can add an arbitrary structured <div> to the map: 您可以向地图添加任意结构化的<div>

var control = document.createElement('div'); 

You add a listener to this control or to its children, eg: 您可以向此控件或其子控件添加侦听器,例如:

google.maps.event.addDomListener(control, 'click', function() {...}); 

You position the control on the map: 您将控件放置在地图上:

control.index = 1;   
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);  

For details, see: 有关详细信息,请参见:

Custom Controls 自定义控件

Example

I don't think you need to add any listener to the map at all. 我认为您根本不需要在地图上添加任何侦听器。 Just add it to the HTML element(s) in your custom control. 只需将其添加到自定义控件中的HTML元素即可。

You certainly want to add your custom logos and buttons and copyrights to Google Maps via custom controls. 您当然想通过自定义控件将自定义徽标,按钮和版权添加到Google地图。 Otherwise, they will likely not render properly especially on mobile devices. 否则,它们可能无法正确渲染,尤其是在移动设备上。

I found the official Google Maps JavaScript API Custom Control example to be rather complicated and I can never remember the position options. 我发现官方的Google Maps JavaScript API自定义控件示例相当复杂,而且我永远都不记得位置选项。 So, I created a tiny 1.72 KB add-on JS on GitHub library called CONTROL-JS that enables you to simply create your custom content as a string, eg, var html = "<h1>Hi</h1>" then pass it to an object called control where every position is a method (IDE intellisense reminds you of the possible positions). 因此,我在GitHub创建了一个很小的1.72 KB附加JS,称为CON​​TROL-JS,使您可以简单地将自定义内容创建为字符串,例如var html = "<h1>Hi</h1>"然后将其传递到称为control的对象,其中每个位置都是一个方法(IDE智能感知会提醒您可能的位置)。

So, in your case, unsing CONTROL-JS just do 因此,就您而言,取消使用CONTROL-JS

var html = '<p id="control-text"> Zoom </p>'

//Global method that is fired when the API is loaded and ready
function initMap () {
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    //intelleSense/Auto-complete works on IDE's
    control.topCenter.add(html);
};

在此处输入图片说明

 /* control.js v0.1 - Add custom controls to Google Maps with ease Created by Ron Royston, www.rack.pub https://github.com/rhroyston/control License: MIT control.topCenter.add.(html) */ var control=function(){function o(o){this.add=function(T){var t=document.createElement("div");if(t.innerHTML=T,o)switch(o){case"TOP_CENTER":map.controls[google.maps.ControlPosition.TOP_CENTER].push(t);break;case"TOP_LEFT":map.controls[google.maps.ControlPosition.TOP_LEFT].push(t);break;case"TOP_RIGHT":map.controls[google.maps.ControlPosition.TOP_RIGHT].push(t);break;case"LEFT_TOP":map.controls[google.maps.ControlPosition.LEFT_TOP].push(t);break;case"RIGHT_TOP":map.controls[google.maps.ControlPosition.RIGHT_TOP].push(t);break;case"LEFT_CENTER":map.controls[google.maps.ControlPosition.LEFT_CENTER].push(t);break;case"RIGHT_CENTER":map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(t);break;case"LEFT_BOTTOM":map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(t);break;case"RIGHT_BOTTOM":map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(t);break;case"BOTTOM_CENTER":map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(t);break;case"BOTTOM_LEFT":map.controls[google.maps.ControlPosition.BOTTOM_LEFT].push(t);break;case"BOTTOM_RIGHT":map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(t)}else console.log("err")}}var T={};return T.topCenter=new o("TOP_CENTER"),T.topLeft=new o("TOP_LEFT"),T.topRight=new o("TOP_RIGHT"),T.leftTop=new o("LEFT_TOP"),T.rightTop=new o("RIGHT_TOP"),T.leftCenter=new o("LEFT_CENTER"),T.rightCenter=new o("RIGHT_CENTER"),T.leftBottom=new o("LEFT_BOTTOM"),T.rightBottom=new o("RIGHT_BOTTOM"),T.bottomCenter=new o("BOTTOM_CENTER"),T.bottomLeft=new o("BOTTOM_LEFT"),T.bottomRight=new o("BOTTOM_RIGHT"),T}(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM