谷歌地图中心不稳定

时间:2014-04-05 12:47:58

标签: javascript css google-maps google-maps-api-3

我正在开发一个基于地图的搜索项目,访问http://indiahomeplus.com/mapview.php, 这里地图的中心不稳定,我把它的中心给了kolkata,但它会自动变换到未知的地方。

还有一个 任何人都可以检查这个问题。

这是js。

             //<![CDATA[
                    var map;
                    var markers = [];
                    var infoWindow;
                    var locationSelect;
                    var iCircle;


                    function init() {
                        map = new google.maps.Map(document.getElementById("map"), {
                            center: new google.maps.LatLng(22.607672, 88.399901),
                            zoom: 12,
                            mapTypeId: 'roadmap',
                           minZoom: 8,

                            mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
                        });











                        infoWindow = new google.maps.InfoWindow();

                        locationSelect = document.getElementById("locationSelect");
                        locationSelect.onchange = function () {
                            var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
                            if (markerNum != "none") {
                                google.maps.event.trigger(markers[markerNum], 'click');
                            }
                        }; searchLocationsNear(new google.maps.LatLng(22.607672, 88.399901));
                         //alert("in init");
                        var distanceWidget = new DistanceWidget(map); 
                       // getmarkers();
                    }

                    function searchLocations() {
                        var address = document.getElementById("addressInput").value;
                        var geocoder = new google.maps.Geocoder();
                        geocoder.geocode({ address: address }, function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {
                                searchLocationsNear(results[0].geometry.location);

                            } else {
                                alert(address + ' not found');
                            }
                        });
                    }

                    function clearLocations() {
                        infoWindow.close();
                        for (var i = 0; i < markers.length; i++) {
                            markers[i].setMap(null);
                        }
                        markers.length = 0;

                        locationSelect.innerHTML = "";
                        var option = document.createElement("option");
                        option.value = "none";
                        option.innerHTML = "See all results:";
                        locationSelect.appendChild(option);
                    }

                    function searchLocationsNear(center) {
                        clearLocations();

                        var radius = document.getElementById('radiusSelect').value;
                        var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
                        downloadUrl(searchUrl, function (data) {
                            var xml = parseXml(data);
                            var markerNodes = xml.documentElement.getElementsByTagName("property");
                            var bounds = new google.maps.LatLngBounds();
                            for (var i = 0; i < markerNodes.length; i++) {
                                var name = markerNodes[i].getAttribute("name");
                                var pid = markerNodes[i].getAttribute("pid");
                                //var address = markerNodes[i].getAttribute("address");
                                var distance = parseFloat(markerNodes[i].getAttribute("distance"));
                                var latlng = new google.maps.LatLng(
                               parseFloat(markerNodes[i].getAttribute("lat")),
                               parseFloat(markerNodes[i].getAttribute("lng")));
                                //alert(pid);
                                createOption(name, distance, i);
                                createMarker(latlng, name,pid);
                                bounds.extend(latlng);
                               // sidebar(pid, latlng);
                            }
                            map.fitBounds(bounds);
                           // locationSelect.style.visibility = "visible";
                            locationSelect.onchange = function () {
                                var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
                                google.maps.event.trigger(markers[markerNum], 'click');
                            };
                        });
                    }

                    function createMarker(latlng, name,pid) {
                        var html = "<b>" + name +","+pid+ "</b>";
                        var marker = new google.maps.Marker({
                            map: map,
                            position: latlng,
                            visible:false //, icon:'images/marker_p1.png'


                        });
                        google.maps.event.addListener(marker, 'click', function () {

                             show_right_div(pid);
                            //showxx();
                        });
                        google.maps.event.addListener(marker, 'mouseover', function () {
                            infoWindow.setContent(html);
                            infoWindow.open(map, marker);
                           // alert(marker);
                        });
                        google.maps.event.addListener(marker, 'mouseout', function () {
                            infoWindow.close();
                        });

                        markers.push(marker);
                    }

                    function createOption(name, distance, num) {
                        var option = document.createElement("option");
                        option.value = num;
                        option.innerHTML = name + "(" + distance.toFixed(1) + ")";
                        locationSelect.appendChild(option);
                    }

                    function downloadUrl(url, callback) {
                        var request = window.ActiveXObject ?
                           new ActiveXObject('Microsoft.XMLHTTP') :
                           new XMLHttpRequest;

                        request.onreadystatechange = function () {
                            if (request.readyState == 4) {
                                request.onreadystatechange = doNothing;
                                callback(request.responseText, request.status);
                            }
                        };

                        request.open('GET', url, true);
                        request.send(null);
                    }

                    function parseXml(str) {
                        if (window.ActiveXObject) {
                            var doc = new ActiveXObject('Microsoft.XMLDOM');
                            doc.loadXML(str);
                            return doc;
                        } else if (window.DOMParser) {
                            return (new DOMParser).parseFromString(str, 'text/xml');
                        }
                    }

                    function doNothing() { }
                     google.maps.event.addDomListener(window, 'load', init);

                    //]]>

                    function hide_right_div() {
                    right_div.style.visibility = "hidden";

                }






                     function show_right_div(str) {

                    right_div.style.visibility = "visible";
                     boxclose.style.visibility="visible"


                     if (str == "") 
                    {
                        document.getElementById("right_div").innerHTML = "";
                        return;
                    }
                    if (window.XMLHttpRequest) 
                    {// code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp = new XMLHttpRequest();
                    }
                    else 
                    {// code for IE6, IE5
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange = function () 
                    {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                         {
                            document.getElementById("right_div").innerHTML = xmlhttp.responseText;

                            resp = xmlhttp.responseText;
                              parseScript(resp);
                        }
                    }
                    xmlhttp.open("GET", "getproperty_view.php?q=" + str, true);
                    xmlhttp.send();

                     //parseScript(resp);

                }






               function parseScript(strcode) {

              var scripts = new Array();         // Array which will store the script's code

              // Strip out tags
              while(strcode.indexOf("<script") > -1 || strcode.indexOf("</script") > -1) {
                var s = strcode.indexOf("<script");
                var s_e = strcode.indexOf(">", s);
                var e = strcode.indexOf("</script", s);
                var e_e = strcode.indexOf(">", e);

                // Add to scripts array
                scripts.push(strcode.substring(s_e+1, e));
                // Strip from strcode
                strcode = strcode.substring(0, s) + strcode.substring(e_e+1);
              }

              // Loop through every script collected and eval it
              for(var i=0; i<scripts.length; i++) {
                try {
                  eval(scripts[i]);
                }
                catch(ex) {
                  // do what you want here when a script fails
                }
              }
            }

1 个答案:

答案 0 :(得分:2)

您的脚本的工作流程为:

  1. 致电init,在那里创建地图并致电...
  2. searchLocationsNear ...您向
  3. 发送请求的位置
  4. http://indiahomeplus.com/phpsqlsearch_genxml.php?lat=22.607672&lng=88.399901&radius=25

    此脚本的回复是...... {/ p>

  5. 中的Not connected :
  6. 回调上述请求,您创建一个LatLngBounds - 对象并根据响应扩展它的位置(这不会发生,因为响应不包含标记)。之后你打电话给...
  7. map.fitBounds(bounds); google.maps.LatLngBounds的初始界限似乎是

    ((1,180),(-1,-180))
    

    ...地图的视口将设置为适合这些边界,这是您看到的结果。

  8. 可能的解决方案:在没有标记的情况下立即退出回调:

    if(!markerNodes.length)return;