temporary_user_name
temporary_user_name

Reputation: 37098

Find the fatal flaw in this google maps javascript code?

Some of this code is borrowed, so I have a little trouble following it due to unfamiliarity with the process of converting an image to a draggable and codable object in the map.

This is part of a phonegap webapp. When run, I see the map and I see the marker image, which I can then drag around-- however, when I drop it, it does not fasten to the map and become clickable for event triggering as in the original example I modeled it after. Can anyone spot any raging flaws?

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />

        <meta charset="utf-8">
            <style type="text/css">
                html { height: 100% }
                body { height: 100%; margin: 0; padding: 0 }
                #map { height: 100% }
                </style>


            <!-- iPad/iPhone specific css below, add after your main css >
             <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />       
             <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />      
             -->
            <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
            <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
            <script type="text/javascript">


                function onBodyLoad()
                {       
                    document.addEventListener("deviceready", onDeviceReady, false);
                }

                /* When this function is called, PhoneGap has been initialized and is ready to roll */
                /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
                 see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
                 for more details -jm */


            <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAHvl0q_ICxMdoioV8TAVeDhT3Rnec_7H_Z7LsYhQbN1_UW7UedxRVF7XTuKiZUL-yUwkvJri9Ou5rRQ" type="text/javascript"></script>
            <script>

                    <script>
                        function centerAndZoomOnBounds(bounds) {
                            var center = bounds.getCenter();
                            var newZoom = map.getBoundsZoomLevel(bounds);
                            if (map.getZoom() != newZoom) {
                                map.setCenter(center, newZoom);
                            } else {
                                map.panTo(center);
                            }
                        }

                        function createMarker(p) {
                            var m = new GMarker(p);
                            GEvent.addListener(m,"click",function(){this.openInfoWindow(p.toUrlValue())});
                            return m;
                        }

                        function addMarkerToMap() {
                            // find location of point
                            var markerPointx = this.left+10; // left+half the width;
                            var markerPointy = this.top+34 ;  // top + height;
                            var markerPoint = map.fromContainerPixelToLatLng(new GPoint(markerPointx,markerPointy));
                            // add marker
                            var m1 = createMarker(markerPoint);
                            map.addOverlay(m1);
                            // move draggable back to start
                            marker.moveTo(new GPoint(16,110));
                        }

                    </script>




            </head>


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

        <p><big><a href="InputCoordinates.html">Back</a></big></p>


        <div id="map" width=100% height=100%>
            <br><p style="padding-left:25px" id="message">You should be seeing a Google Map here.</p>

        </div>

        <img src="http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png" id="imgMarker">

            <script type="text/javascript">
                //<![CDATA[

                var mapdiv = document.getElementById("map");
                var map = new GMap2(mapdiv);

                map.addControl(new GSmallMapControl());
                map.addControl(new GScaleControl());
                map.addControl(new GMapTypeControl());
                map.setCenter(new GLatLng(52.019029,-0.770427), 6, G_NORMAL_MAP);

                marker = new GDraggableObject(document.getElementById("imgMarker"));
                marker.moveTo(new GPoint(16,110));
                GEvent.addListener(marker,"dragend",addMarkerToMap);

                //]]>
            </script>

        <!--Maps API Key
         AIzaSyD52tRfTY1FtEgXQpvu9-jN5AOz-2pxQg0
         -->
    </body>


</html>

Upvotes: 0

Views: 201

Answers (1)

javram
javram

Reputation: 2645

The first problem is that the onload method on the body points to a non existent function: initialize();

Also, the map initialization code needs to be done inside of a function that is called when the body onload event fires, so that the necessary dom elements are guaranteed to have been created.

Here is a basic example that shows about the simplest maps initialization that can be run.

https://google-developers.appspot.com/maps/documentation/javascript/examples/map-simple

Upvotes: 1

Related Questions