Google Groups Home
Help | Sign in
newbie question on fromLatLngToPixel
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
PK  
View profile
 More options Feb 25, 1:00 pm
From: PK <pushpak.karn...@gmail.com>
Date: Mon, 25 Feb 2008 10:00:31 -0800 (PST)
Local: Mon, Feb 25 2008 1:00 pm
Subject: newbie question on fromLatLngToPixel
Hi,

 I am trying to create custom html markers (using DIV elements and
absolute positioning) on a GMap2 object based on current driving
directions.

 Initially, everything works fine and the markers are created in the
correct places. However, once I change the 'From' and 'To' fields of
the driving directions object, the coordinates returned to me are very
off from the map.

 I also read the previous thread on this issue:
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/2...
and implemented the suggested code, but it does not work for me. I
actually get "NaN' values.

I am posting my code below.

Any help is greatly appreciated :)
Regards
Pushpak

==  code starts

<!DOCTYPE html "-//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>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/


    <title>RouteMapper</title>
    <script src="http://maps.google.com/maps?
file=api&amp;v=2.x&amp;key=ABQIAAAA4Fup_FpJp-dXcgc7qUCbchQq-KFf-
KlVola20L9ZRYbI-wXuhhQTNDn1lJIsSzWI1qfPR-c6yQF3kg"
            type="text/javascript"></script>
    <script src="http://gmaps-utility-library.googlecode.com/svn/trunk/
markermanager/release/src/markermanager.js"></script>
    <script type="text/javascript">

    var map;
    var gdir;
    var center;
    var map_child;

    // create placeholders for the maps ...
    function createDiv( x, y, width, height, sid )
    {
        var eDiv=document.createElement("DIV");
        eDiv.style.width=width;
        eDiv.style.height=height;
        eDiv.style.position="absolute";
        eDiv.style.backgroundColor="coral";
        eDiv.style.left=x;
        eDiv.style.top=y;
        eDiv.id = "map_parent";

        var cDiv = document.createElement("DIV");
        cDiv.style.width=width-4;
        cDiv.style.height=height-4;
        cDiv.style.position="absolute";
        cDiv.style.left=2;
        cDiv.style.top=2;
        cDiv.id = sid;

        eDiv.appendChild( cDiv );

        return eDiv;
    }

    // add GMap object to the placeholder
    function createMapForID( sid, point, zoom_level )
    {
       map_child = new GMap2(document.getElementById(sid));
       map_child.setCenter( point, zoom_level );
       map_child.addOverlay( new GMarker( point ) );
    }

    // create the base map as background
    function createBaseMap()
    {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.enableContinuousZoom();
        map.addControl(new GScaleControl());
        map.addControl(new GSmallMapControl());
    }

    // initialize the map object
    function initialize()
    {
      if (GBrowserIsCompatible())
      {
        createBaseMap();
        gdir = new GDirections( map,
document.getElementById("dir_text") );
            GEvent.addListener(gdir, "load", gdirLoad);
      }
    }

    // set the directions here
    function setDirections( fromAddress, toAddress )
    {
        gdir.clear();
        map.clearOverlays();
            gdir.load("from: " + fromAddress + " to: " + toAddress );
    }

    // Called when the directions are loaded
    function gdirLoad()
    {
        // the directions should have their bounding box defined
        var bounds          = gdir.getBounds();
        var center_LatLng   = bounds.getCenter();

        map.panTo( center_LatLng );

        bounds              = map.getBounds();
        center_LatLng       = bounds.getCenter();

            // Now display the number of routes
            var numRoutes = gdir.getNumRoutes();

            var i;
            for( i = 0; i < numRoutes; ++i )
            {
                var Rt = gdir.getRoute(i);
                var j;
                for( j = 0; j < Rt.getNumSteps(); ++j )
                {
                    var stp         = Rt.getStep(j);
                    var latLng      = stp.getLatLng();
                    map.addOverlay( new GMarker( latLng ) );

                    alert( latLng );
                    // get bottom left (south west) point
                    var sw          =
map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().g etSouthWest(),map.getZoom());
                    alert( sw );
                    // get the pixel coordinates
                var px_coords   = latLng - sw;

                    // var px_coords =
map.fromContainerPixelToLatLng( latLng );
                    alert( px_coords );

                    document.body.appendChild( createDiv( px_coords.x,
px_coords.y, 200, 200, "marker_" + j ) );
                }
            }
    }

    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()" >

    <!-- The Start and End locations -->
    <form action="#" onsubmit="setDirections(this.from.value,
this.to.value); return false">
        <table>
        <tr>
                <th align="right">From:&nbsp;</th>
                <td><input type="text" size="100" id="fromAddress" name="from"
value="Tempe"/></td>

                <th align="right">&nbsp;&nbsp;To:&nbsp;</th>
                <td align="right"><input type="text" size="100" id="toAddress"
name="to"  value="Phoenix" /></td>
        </tr>
        <tr>
        <td><input name="submit" type="submit" value="Get Directions!" /></
td>
        </tr>

        </table>
    </form>

    <table>
    <tr>
    <!-- The map image -->
    <th><div id="map_canvas" style="width: 1200px; height: 768px"></
div></th>
    <!-- The textual directions -->
    <th><div id="dir_text"   style="width: 500px; height: 768px"></
div></th>
    </tr>
    </table>

  </body>
</html>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
maps.huge.info [Maps API Guru]  
View profile
 More options Feb 25, 1:04 pm
From: "maps.huge.info [Maps API Guru]" <cor...@gmail.com>
Date: Mon, 25 Feb 2008 10:04:23 -0800 (PST)
Local: Mon, Feb 25 2008 1:04 pm
Subject: Re: newbie question on fromLatLngToPixel
Please read and follow the posting guidelines. Thanks.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Williams  
View profile
 More options Feb 25, 2:25 pm
From: Mike Williams <nos...@econym.demon.co.uk>
Date: Mon, 25 Feb 2008 19:25:00 +0000
Subject: Re: newbie question on fromLatLngToPixel
API v2.100 introduces map.fromLatLngToContainerPixel(latlng)

If you use that, then much of your confusion between DivPixel and
ContainerPixel will go away.

--
http://econym.googlepages.com/index.htm
The Blackpool Community Church Javascript Team


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google