如何在谷歌地图中的标记之间画一条线

时间:2013-10-10 07:35:12

标签: javascript mysql google-maps google-maps-api-2

我有一个谷歌地图,我必须将数据从数据库显示到谷歌地图。我已经这样做了我从mysql数据库获取数据到谷歌地图。所有的城市都从数据库填充,现在我必须绘制我在数据库中填充的城市之间的一条线。这是我到目前为止所做的代码..

 <script type="text/javascript">

        /*This is */
        function getCallbackFunction(req, processData) {

            // Return an anonymous function that listens to the 
            // XMLHttpRequest instance
            return function () {

                // If the request's status is "complete"
                if (req.readyState == 4) {
                    if (req.status == 200) {

                        // Pass the XML payload of the response to the 
                        // handler function
                        processData(req.responseXML);

                    } else {

                        // An HTTP problem has occurred
                        alert("HTTP error: "+req.status);
                    }
                }
            }
        }


        //]]>
    </script>

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


        var gmarkers = [];

        function getMarker(id) {
            for (var i = 0; i <= gmarkers.length; i++) {
                var marker = gmarkers[i];
                if (marker.id == id) {
                    return marker;
                }
            }
            return null;
        }

        function load() {
            if (GBrowserIsCompatible()) {
                var i = 0;
                var baseIcon = new GIcon();
                baseIcon.iconSize=new GSize(12,12);
                baseIcon.shadowSize=new GSize(12,12);
                baseIcon.iconAnchor=new GPoint(6,6);
                baseIcon.infoWindowAnchor=new GPoint(6,6);

                var pinIcon = new GIcon(baseIcon, "http://maps.google.co.uk/intl/en_ALL/mapfiles/ms/micons/red-dot.png", null, null);

                // A function to create the marker and set up the event window
                function createLibraryMarker(point,name,id) {

                    var baseIcon = new GIcon();
                    baseIcon.iconSize=new GSize(20,12);
                    baseIcon.shadowSize=new GSize(20,12);
                    baseIcon.iconAnchor=new GPoint(10,6);
                    baseIcon.infoWindowAnchor=new GPoint(10,0);

                    var icon = new GIcon(baseIcon, "images/smaller-library-icon.gif", null, null);

                    var marker = new GMarker(point,icon);
                    // === store the name so that the tooltip function can use it ===
                    marker.tooltip = '<div class="tooltip">'+name+'</div>';
                    marker.id = id;
                    GEvent.addListener(marker, "click", function() {
                        openInfoWindow(marker,"" + id);
                    });
                    gmarkers[i] = marker;

                    i++;
                    map.addOverlay(marker);

                    //  ======  The new marker "mouseover" and "mouseout" listeners  ======
                    GEvent.addListener(marker,"mouseover", function() {
                        showTooltip(marker);
                    });        
                    GEvent.addListener(marker,"mouseout", function() {
                        tooltip.style.visibility="hidden"
                    });        
                }





                //
                // The list of city markers, loaded only once
                //
                var cityMarkers = null;

                function displayCities() {
                    if (cityMarkers == null) {
                        loadCities();
                    } else {
                        displayCityMarkers();
                    }
                }

                function loadCities() {
                    // Read the data from data.xml
                    var request = GXmlHttp.create();
                    request.open("GET", "/maps/LibraryDirectory?method=findAllCities", true);
                    request.onreadystatechange = getCallbackFunction(request, processCityData);
                    request.send(null);
                }

                /**
                 * Process the city list in XML form, store it in cityMarkers, 
                 * and display the markers.
                 */           
                function processCityData(xmlDoc) {
                    cityMarkers = xmlDoc.documentElement.getElementsByTagName("marker");                      
                    displayCityMarkers();
                }

                function displayCityMarkers() {
                    map.clearOverlays();
                    for (var i = 0; i < cityMarkers.length; i++) {
                        // obtain the attribues of each marker
                        var lat = parseFloat(cityMarkers[i].getElementsByTagName("latitude")[0].firstChild.nodeValue);
                        var lng = parseFloat(cityMarkers[i].getElementsByTagName("longitude")[0].firstChild.nodeValue);
                        var id = cityMarkers[i].getElementsByTagName("id")[0].firstChild.nodeValue;
                        var label = cityMarkers[i].getElementsByTagName("name")[0].firstChild.nodeValue;

                        // create the marker


                        createCityMarker(new GLatLng(lat,lng),label,id);


                    }

                }


                function createCityMarker(point,name,id) {

                    var marker = new GMarker(point,pinIcon);
                    // === store the name so that the tooltip function can use it ===
                    marker.tooltip = '<div class="tooltip">'+name+'</div>';
                    marker.id = id;
                    marker.point = point;
                    GEvent.addListener(marker, "click", function(point) {
                        map.setCenter(new GLatLng(marker.point.y,marker.point.x),10);
                    });
                    gmarkers[i] = marker;

                    i++;
                    map.addOverlay(marker);

                    //  ======  The new marker "mouseover" and "mouseout" listeners  ======
                    GEvent.addListener(marker,"mouseover", function() {
                        showTooltip(marker);
                    });        
                    GEvent.addListener(marker,"mouseout", function() {
                        tooltip.style.visibility="hidden"
                    });        
                }


                // ====== This function displays the tooltip ======
                function showTooltip(marker) {
                    tooltip.innerHTML = marker.tooltip;
                    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
                    var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
                    var anchor=marker.getIcon().iconAnchor;
                    var width=marker.getIcon().iconSize.width;
                    var height=tooltip.clientHeight;
                    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
                    pos.apply(tooltip);
                    tooltip.style.visibility="visible";
                }

                // ===== This function is invoked when the mouse leaves an entry in the sidebar =====
                // It hides the tooltip      
                function mymouseout() {
                    tooltip.style.visibility="hidden";
                }

                var myTrip = {
                    center:x,
                    zoom:4,
                    mapTypeId:google.maps.MapTypeId.ROADMAP
                };

                // create the map
                var map = new google.maps.Map(document.getElementById("map"),myTrip);

                map.addControl(new GLargeMapControl());
                map.addControl(new GOverviewMapControl());
                map.enableDoubleClickZoom();
                map.setCenter(new GLatLng(28.6100, 77.2300), 5);

                // ====== set up marker mouseover tooltip div ======
                var tooltip = document.createElement("div");
                map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
                tooltip.style.visibility="hidden";

                displayCities();
                GEvent.addListener(map, "zoomend", function(oldZoom, newZoom) {
                    if ((oldZoom <= 8) && (newZoom > 8)) {
                        //
                        // Switch to city markers
                        //
                        displayLibraries();
                    } else if ((oldZoom > 8) && (newZoom <= 8)) {
                        //
                        // Switch to library markers
                        //
                        displayCities();
                    }

                });
            }

            else {
                alert("Sorry, the Google Maps API is not compatible with this browser");
            }
        }
        //]]>
    </script>

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

var lat,var lng这是来自database.i的纬度和经度必须使用此lat long绘制线条, 有人请帮忙

1 个答案:

答案 0 :(得分:0)

一般的答案是通过cityMarkers数组处理从他们的位置创建一个google.maps.LatLng对象数组。使用该数组创建折线。

该答案适用于v2或v3;详细信息将取决于您最终使用的API(但作为v2 is deprecated and scheduled to be turned off/transitioned to a wrapped v3 version that might not work for you,您最好在v3中编写任何新代码。)