Click on the label to display the description on Google Maps and keep it intact

Source: Internet
Author: User

JS as follows:

(function () {

???? Window.onload = function () {

???????? Creating an object literal containing the properties

???????? We want to pass to the map

???????? var options = {

???????????? Zoom:3,

???????????? Center:new Google.maps.LatLng (37.09,-95.71),

???????????? MapTypeId:google.maps.MapTypeId.ROADMAP

????????};

???????? Creating the Map

???????? var map = new Google.maps.Map (document.getElementById (' map '), options);

???????? Creating An array that would contain the coordinates

???????? For New York, San Francisco, and Seattle

???????? var places = [];

???????? Adding a Latlng object for each city

???????? Places.push (New Google.maps.LatLng (40.756,-73.986));

???????? Places.push (New Google.maps.LatLng (37.775,-122.419));

???????? Places.push (New Google.maps.LatLng (47.620,-122.347));

???????? Looping through the Places array

???????? for (var i = 0; i < places.length; i++) {

???????????? Adding the marker as usual

???????????? var marker = new Google.maps.Marker ({

???????????????? Position:places[i],

???????????????? Map:map,

???????????????? Title: ' Place number ' + I

????????????});

???????????? Wrapping the event listener inside an anonymous function

???????????? That we immediately invoke and passes the variable i to.

???????????? (Function (i, marker) {

???????????????? Creating the event listener. It now have access to the values of

???????????????? I and marker as they were during its creation

???????????????? Google.maps.event.addListener (marker, ' click ', function () {

???????????????????? var Infowindow = new Google.maps.InfoWindow ({

???????????????????????? Content: ' Place number ' + I

????????????????????});

???????????????????? Infowindow.open (map, marker);

????????????????});

????????????}) (I, marker);

????????}

????}

})();

CSS as follows:

Body{

font-family< Span style= "COLOR: #38444b" >:verdana ,geneva , arial, helvetica,sans-serif;

Font-size:small;

Background:#fff;

}

#map{

Width:%;

Height:px;

Border:1pxsolid#000;

}

. info{

Width:+px;

}

?

HTML as follows:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-type" Content="text/html; Charset=utf-8 " />

<title>my first map</title>

<link type= "text/css" href=" Css/style.css " rel= "stylesheet" media=/>

<script Span style= "COLOR: #cb2d01" >type= "Text/javascript" src= "Http://maps.google.com/maps /api/js?sensor=false "></script >

<script type="Text/javascript" src="Js/map.js"></script>

</head>

<body>

<H1>my first map</H1>

<div id="Map"></div>

</body>

</html>

?

The effect is as follows:

Click on the label to display the description on Google Maps and keep it intact

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.