Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Have label permanently appear on Google Map

Posted on 2014-11-06
5
Medium Priority
?
877 Views
Last Modified: 2014-11-06
Hi all.

I have the code below that displays markers for the addresses in my MySQL table. Currently when you click a marker it will display the name, address, city , state, zip code and type.

I would like for it to show just the name in a label when the map is displayed and if the user clicks a marker then it will display the detail information I listed above (i.e. name, address, city etc.)

How can I do this?

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Builder Community Map</title>    
    <script type="text/javascript" src="<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    

    var customIcons = {
      Builder: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
      },
      Community: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(25.976562, -80.377640),
        zoom: 12,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      downloadUrl("GetBuilderCommunityMapAddresses.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var city = markers[i].getAttribute("city");
          var state = markers[i].getAttribute("state");
          var zipcode = markers[i].getAttribute("zipcode");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          //var html = "<b>" + name + "</b> <br/>" + address;
          var html = "<b>" + name + "</b> <br/>" + address+ "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + type;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon
          });
          
          bindInfoWindow(marker, map, infoWindow, html);
          
         
          
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

  </script>

  </head>

  <body onload="load()">
    <div id="map" style="width: 800px; height: 900px"></div>
  </body>

</html>

Open in new window

0
Comment
Question by:Sim1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40427370
0
 

Author Comment

by:Sim1980
ID: 40427566
Ok. I tried using the MarkerWithLabel in my code below, but it doesn't show the labels and it does not show the markers. When I comment out the MarkerWithLabel and use Marker instead then I see the markers and obviously no labels.

What am I doing wrong with the code?

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Builder Community Map</title>  
    
    <style type="text/css">
   .labels {
     color: red;
     background-color: white;
     font-family: "Lucida Grande", "Arial", sans-serif;
     font-size: 10px;
     font-weight: bold;
     text-align: center;
     width: 40px;     
     border: 2px solid black;
     white-space: nowrap;
   }
 </style>
      
   <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="markerwithlabel.js"></script>
    
    
    <script type="text/javascript">

    var customIcons = {
      Builder: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
      },
      Community: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(33, -33),
        zoom: 12,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      downloadUrl("GetBuilderCommunityMapAddresses.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var city = markers[i].getAttribute("city");
          var state = markers[i].getAttribute("state");
          var zipcode = markers[i].getAttribute("zipcode");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          var html = "<b>" + name + "</b> <br/>" + address+ "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + type;
          var icon = customIcons[type] || {};
          
          //var marker = new google.maps.Marker({
         var marker = new google.maps.MarkerWithLabel({          
            position: point,
            map: map,  
           icon: icon.icon,
           labelContect: name,
           labelAnchor: new google.maps.Point(22,0),
           labelClass: "labels", // the CSS class for the label
		   labelStyle: {opacity: 1.0}
          });
                 
          
          bindInfoWindow(marker, map, infoWindow, html);         
          
        }
      });
    }
         

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

  </script>

  </head>

  <body onload="load()">
    <div id="map" style="width: 800px; height: 900px"></div>
  </body>

</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40427574
could you post what you get from GetBuilderCommunityMapAddresses.php ?
0
 

Author Comment

by:Sim1980
ID: 40427586
Sure, below is what I get, but I changed the addresses as that is sensitive information.

<markers>
<marker name="Builder A" address="123 SW 143RD WAY" city="MyCity" state="MA" zipcode="00023" lat="30" lng="-80" type="Builder"/>
<marker name="Builder B" address="333 SW 33 ST" city="YourCity" state="NY" zipcode="12222" lat="40" lng="-80" type="Builder"/>
<marker name="MMA" address="5555 N 44 ST" city="OurCity" state="TX" zipcode="56677" lat="22" lng="-80" type="Community"/>
<marker name="aa" address="789 W 43 Ct" city="City A" state="CA" zipcode="55666" lat="23" lng="-80" type="Community"/>
<marker name="MM" address="44444 W 3rd ave" city="Lala" state="OR" zipcode="45667" lat="67" lng="-80" type="Community"/>
</markers>

Open in new window

0
 

Author Comment

by:Sim1980
ID: 40427593
Nevermind, got it to work.

I had to change the line to: var marker = new MarkerWithLabel

Instead of var marker = new new google.maps.MarkerWithLabel

And I had to fix  labelContent I had originally typed it as labelContect.

Thanks again for all your help!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question