1

在下面的代码中,谷歌地图总是只显示巴黎而不显示输入的位置。

在下面的表单元素中,我正在调用 javascript showAddress(location) 函数。

HTML正文:

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

<form action="#" method="post" onsubmit="showAddress(this.address.value); return false">
<input type="text" name="address" size="40" value="Paris"/>
<input type="submit"/>
</form>
<div id="map" style="width:500px;height:400px"></div>
</body>

脚本:

<script type="text/javascript">
    function loadDefaultMap(){
        if(GBrowserIsCompatible()){

            var center =  new GLatLng(48.85661, 2.35222); //Paris

            var map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(center,15);

            geoCoder = new GClientGeocoder();   // this will render the map on th bases of address entered

            var marker = new GMarker(center, {draggable: true});  
            map.addOverlay(marker);

            GEvent.addListener(marker, "dragend", function() {
                var point = marker.getPoint();
                map.panTo(point);
            });

            GEvent.addListener(map, "moveend", function() {
                map.clearOverlays();
                var center = map.getCenter();
                var marker = new GMarker(center, {draggable: true});
                map.addOverlay(marker);

                GEvent.addListener(marker, "dragend", function() {
                    var point =marker.getPoint();
                    map.panTo(point);
                });
            });
        }
    }


    function showAddress(address){
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());

        if(geoCoder){
            geoCoder.getLatLng(address, function(point){
                if(!point){
                    alert('Location Not Available');
                }else{
                    map.clearOverlays();
                    map.setCenter(point,15);
                    var marker = new GMarker(address, {draggable: true});  
                    map.addOverlay(marker);
                }
            })


        }
    }

    </script>

提前致谢 :)

4

1 回答 1

0

map.setCenter()接受一个GLatLng对象,而不是一个point. 尝试GLatLng从您的point. 如果点包含纬度和经度字段,您可以执行以下操作:

map.setCenter(new GLatLng(point.latitude, point.longitude), 15);
于 2012-05-30T20:39:46.470 回答