How to use javascript closure to implement multi-point labeling bubble example _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to use the js closure method to realize multi-point labeling and bubbling. If you need it, you can refer to the two days of map creation, a little js Code and various pitfalls. The first time I came into contact with js, I had various difficulties. Next I will make some conclusions on the research over the past few days to find the difficulties.
Use closures in Event Listeners

When executing an event listener, you can usually append private data and persistent data to an object. JavaScript does not support "private" instance data, but allows internal functions to access the closure of external variables. In event listeners, closures are very suitable for accessing variables that are usually not appended to objects where events occur.

The following example uses a function closure in the event listener to allocate encrypted messages to a group of tags. Click each tag to view a part of the encrypted message, which is not included in the tag.

The Code is as follows:


Var map;
Function initialize (){
Var myLatlng = new google. maps. LatLng (-25.363882, 131.044922 );
Var mapOptions = {
Zoom: 4,
Center: myLatlng,
MapTypeId: google. maps. MapTypeId. ROADMAP
}

Map = new google. maps. Map (document. getElementById ("map_canvas"), mapOptions );

// Add 5 markers to the map at random locations.
Var southWest = new google. maps. LatLng (-31.203405, 125.244141 );
Var northEast = new google. maps. LatLng (-25.363882, 131.044922 );
Var bounds = new google. maps. LatLngBounds (southWest, northEast );
Map. fitBounds (bounds );
Var lngSpan = northEast. lng ()-southWest. lng ();
Var latSpan = northEast. lat ()-southWest. lat ();
For (var I = 0; I <5; I ++ ){
Var location = new google. maps. LatLng (southWest. lat () + latSpan * Math. random (),
SouthWest. lng () + lngSpan * Math. random ());
Var marker = new google. maps. Marker ({
Position: location,
Map: map
});
Var j = I + 1;
Marker. setTitle (j. toString ());
AttachSecretMessage (marker, I );
}
}

// The five markers show a secret message when clicked
// But that message is not within the marker's instance data.
Function attachSecretMessage (marker, number ){
Var message = ["This", "is", "the", "secret", "message"];
Var infowindow = new google. maps. InfoWindow (
{Content: message [number],
Size: new google. maps. Size (50, 50)
});
Google. maps. event. addListener (marker, 'click', function (){
Infowindow. open (map, marker );
});
}


This code was copied from the google official website.

Next, I want to read the latitude and longitude information and address information from the database, mark the information on google map, and click the information that appears.

To implement multi-point tagging, refer to the above Code

The Code is as follows:


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.