"Baidu Map API" How to determine whether the click is a map or a cover?

Source: Internet
Author: User
Tags polyline

The original: "Baidu Map API" How to determine whether the click is a map or cover?

Abstract: Many API enthusiasts ask me why I clicked on marker and map will respond to the event? How can I tell if I clicked on a callout or a map? Take a look below.

------------------------------------------------------------

First look at the map of what events, see the official website class reference, turn to the event:

Did you see the parameters here?

In fact, when you click on an object, you can determine what is being clicked by these parameters.

Example with marker:

First create a marker

// cover Marker    var New Bmap.point (116.249472,39.946583);     var New Bmap.marker (PMK);    Map.addoverlay (MK);

Then add a click event to the map, and if you decide it is the overlay, the corresponding dialog box pops up.

    Map.addeventlistener ("click",function(e) {        if(e.overlay) {            alert (' You clicked on the overlay: ' +e.overlay.tostring ());           } Else {            alert (' you clicked on a map ');        }    });

Add a bunch of overlays and then write a click event for each overlay.

For example, if I click on a polygon, this will pop up.

All source code:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Click events</title><Scripttype= "Text/javascript"src= "http://api.map.baidu.com/api?key=v=1.3"></Script></Head><Body>  <!--Baidu Map Container -  <Divstyle= "Width:697px;height:550px;border: #ccc solid 1px;"ID= "Ditucontent"></Div></Body><Scripttype= "Text/javascript">    varMap= NewBmap.map ("ditucontent"); var Point= NewBmap.point (116.331398,39.897445); Map.centerandzoom (Point, A);    Map.enablescrollwheelzoom (); Map.addeventlistener ("Click",function(e) {if(e.overlay) {alert ('you clicked on the cover:'+e.overlay.tostring ()); }Else{alert ('you clicked on a map.');    }    }); //Cover Marker    varPMK= NewBmap.point (116.249472,39.946583); varMk= NewBmap.marker (PMK);    Map.addoverlay (MK); //Overlay Label    varPLB= NewBmap.point (116.408149,39.958087); varlb= NewBmap.label ('I am the overlay label', {POINT:PLB});    Map.addoverlay (LB); //cover Polyline    varPL= NewBmap.polyline ([NewBmap.point (116.250047,39.919583),NewBmap.point (116.441494,39.919583)],{strokeweight:Ten}) map.addoverlay (PL); //Cover Polygon    varPG= NewBmap.polygon ([NewBmap.point (116.248323,39.893016),NewBmap.point (116.440344,39.893016),NewBmap.point (116.440344,39.811036),NewBmap.point (116.248323,39.811036)]); Map.addoverlay (PG);</Script></HTML>

"Baidu Map API" How to determine whether the click is a map or a cover?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.