Google地图未显示输入的位置

时间:2012-05-30 20:27:43

标签: javascript html google-maps

在下面的代码中,Google地图始终仅显示巴黎&不显示输入的位置。

在下面的表单元素中,我正在调用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>

提前致谢:)

1 个答案:

答案 0 :(得分:0)

map.setCenter()获取GLatLng个对象,而不是point。尝试从GLatLng创建新的point对象。如果point包含纬度和经度字段,您可以执行以下操作:

map.setCenter(new GLatLng(point.latitude, point.longitude), 15);