JavaScript
  Home arrow JavaScript arrow Page 3 - Using Polylines and Smart Markers with Yah...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using Polylines and Smart Markers with Yahoo! Maps
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-29

    Table of Contents:
  • Using Polylines and Smart Markers with Yahoo! Maps
  • Review: adding basic markers to an existing map
  • Drawing polygonal lines (polylines)
  • Using smart markers with a selected Yahoo! Map

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Polylines and Smart Markers with Yahoo! Maps - Drawing polygonal lines (polylines)


    (Page 3 of 4 )

    In the section that you just read, I told you that the Yahoo! Maps framework permits you to draw polylines by using another handy JavaScript class, which, not surprisingly, is called "YPolyline()." As you will see for yourself in a moment, the class is quite flexible and allows you to display these lines using different customization parameters, including line colors, opacity, etc.

    Let me show you a concrete example of how to draw a few simple polygonal lines on an existing Yahoo! Map. The corresponding code sample is as follows:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

    <head>

    <title>Example on building a YAHOO! Map and adding polylines</title>

    <script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?
    v=3.7&appid=Your-AP-ID"></script>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: bold 16pt Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    }

    #mapcontainer{

    height: 400px;

    width: 500px;

    margin-left: auto;

    margin-right: auto;

    }

    </style>

    </head>

    <body>

    <h1>Example on building a YAHOO! Map and adding polylines</h1>

    <div id="mapcontainer"></div>

    <script type="text/javascript">

    function initializeMap(){

    // define array to store polyline points

    polylinePoints=new Array();

    // Create a new map object

    var map=new YMap(document.getElementById('mapcontainer'));

    // Add type controls to the map

    map.addTypeControl();

    // Add the Pan Control to the map

    map.addPanControl();

    // Add long zoom control to the map

    map.addZoomLong();

    // Set map type to YAHOO_MAP_SAT

    map.setMapType(YAHOO_MAP_SAT);

    // Display the map centered on the selected location

    map.drawZoomAndCenter("Los Angeles",3);

    // Add an event to report to our Logger

    YEvent.Capture(map,EventsList.MouseClick,drawPolyline);

    // draw polyline when clicking on the map

    function drawPolyline(_e,_c){

    var geoPoint=new YGeoPoint(_c.Lat,_c.Lon);

    // add marker to the map

    map.addMarker(geoPoint);

    polylinePoints.push(geoPoint);

    // draw polyline on the map

    map.addOverlay(new YPolyline(polylinePoints,'black',10,0.5));

    }

    }

    window.onload=function(){

    initializeMap();

    }

    </script>

    </body>

    </html>


    As you can see, the above hands-on example first constructs a simple satellite map, and then uses the same click logger that you learned in the previous section to dynamically include different markers as the map is being clicked on. So far, this process should be pretty familiar to you. Please notice that I also defined a "polylinePoints" array with the purpose of storing the different latitude/longitude values corresponding to each mouse click.

    These array elements are then used to create several black polygonal lines, which are inserted into the map by way of another JavaScript method, called "addOverlay()." As you may guess, this method is utilized to "overlap" different elements on a given map, and certainly polylines are no exception.

    If you're anything like me, then you may want to see the final result of this process. Below I included a screen shot that shows the polylines in action. Here it is:



    As you can see, the pertinent polygonal lines come in handy for interconnecting different markers as they are displayed on the map. Quite simple to grasp, right?

    So far, so good. At this point, you should have an exact idea of how to draw a few basic polylines in an existing Yahoo! Map. So the next step will consist of demonstrating how to build special markers, which in the context of the Yahoo! Map framework are called "smart markers."

    As you'll see in the section to come, these special markers can include additional markup in their structures. Therefore, jump forward and keep reading, since I'll be there waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This fifth chapter of this series demonstrates with several code samples, how to...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway