找到这个谷歌地图javascript代码中的致命缺陷?

时间:2012-03-22 15:44:50

标签: google-maps-api-3

这些代码中的一部分是借来的,所以由于不熟悉将图像转换为地图中可拖动和可编码的对象的过程,我有点麻烦。

这是phonegap webapp的一部分。运行时,我看到地图,然后我看到标记图像,然后我可以拖动它 - 但是,当我放下它时,它不会固定到地图上,并且可以点击事件触发,就像在原始示例中我建模的那样后。谁能发现任何肆虐的瑕疵?

                          

        <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>

1 个答案:

答案 0 :(得分:1)

第一个问题是body上的onload方法指向一个不存在的函数:initialize();

此外,地图初始化代码需要在body onload事件触发时调用的函数内部完成,以便保证创建必要的dom元素。

这是一个基本示例,显示了可以运行的最简单的映射初始化。

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