如何刷新谷歌地图?

时间:2013-02-27 10:52:20

标签: google-maps iframe

当用户根据这个新位置在文本字段中输入新的位置地址时,我需要使用Google地图刷新iframe。

我的HTML代码:

  <input type="text" id="address" />
<input type="button" id="search" value="Search" onclick="javascript:findLocation();"/>
<iframe id="mapView" width="700" height="385" 
    frameborder="0" scrolling="no" 
    marginheight="0" marginwidth="0" 
    src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Kharkiv&amp;aq=&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear=Kharkiv&amp;z=14&amp;output=embed">
</iframe>

我的JavaScript代码:

function findLocation(){
    var location=document.getElementById('address').value;
    var newLocation="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q="+location+"&amp;aq=&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear="+location+"&amp;z=14&amp;output=embed";
    document.getElementById("mapView").src=newLocation;
    document.getElementById("mapView").reload(true);
}

但iframe只是在按下按钮后禁用。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

使用计时器从舞台中删除地图并重新添加,调用完整的Google地图生命周期:

package
{
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;

import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;

public class Test extends Sprite
{

    private var _map:Map;

    private var _timer:Timer;

    public function Test()
    {
        super();

        createMap();

        _timer = new Timer(300000); // 5-min
        _timer.addEventListener(TimerEvent.TIMER, timerHandler);
        _timer.start();
    }

    private function timerHandler(event:TimerEvent):void
    {
        while (numChildren > 0)
            removeChildAt(0);

        createMap();
    }

    protected function createMap():void
    {
        _map = new Map();
        _map.key = "YOUR_API_KEY";
        _map.sensor = "false";
        _map.setSize(new Point(stage.stageWidth, stage.stageHeight));
        addChild(_map);

        _map.addEventListener(MapEvent.MAP_READY, mapReadyHandler);
    }

    protected function mapReadyHandler(event:MapEvent):void
    {
        _map.removeEventListener(MapEvent.MAP_READY, mapReadyHandler);
        _map.setCenter(new LatLng(37.4419, -122.1419), 13, MapType.NORMAL_MAP_TYPE);
    }

}
}

答案 1 :(得分:0)

尝试google.maps.event.trigger(map, 'resize');它与我合作!