2

我有一个简单的 gmappanel (extjs 4.1.1)。如何更改地图的“中心”并以新坐标的中心刷新我的窗口?

我的代码是:

Ext.Loader.setConfig({
        enabled : true
    });
Ext.require(['Ext.window.*', 'Ext.ux.GMapPanel']);

Ext.define('AM.view.gmapwindow.GoogleMap', {
extend : 'Ext.window.Window',
alias : 'widget.gmapw',
autoShow : true,
title : 'map',
closeAction : 'hide',
width : 460,
height : 500,
border : false,
x : 40,
y : 60,
items : [{
            layout : {
                type : 'hbox',
                align : 'stretch'
            },
            flex : 1,
            items : [{
                        xtype : 'textfield'
                    }, {
                        xtype : 'button',
                         handler: function() {
//--------------------
//modify here the center 
//geoCodeAddr:'Suisse Romande, Avenue de la Gare, Sion, Svizzera'
//---------------------
                            }
                    }]
        }, {
            width : 450,
            layout : 'fit',
            height : 450,
            border : false,
            items : {
                xtype : 'gmappanel',
                center : {
                    geoCodeAddr : '4 Yawkey Way, Boston, MA, 02215-3409, USA'

                },

                markers : []
            }
        }]

});

地图显示得很好,但如果我尝试更改中心编辑

地理编码地址

使用以下代码

this.up('gmapw').down('gmappanel').center.geoCodeAddr='Suisse Romande, Avenue de la Gare, Sion, Svizzera';

没发生什么事。

有任何想法吗?

谢谢

4

2 回答 2

3

如果您查看GMapPanel.js,您会看到在创建地图时使用afterFirstLayout()了in 。geoCodeAddr在布局之后设置它不会做任何事情,因为afterFirstLayout()不会再次被调用。

您应该对您的地址进行地理编码并使用它在地图上设置中心。像下面这样的东西应该可以工作:

var map = this.gmap;
var geocoder = new google.maps.Geocoder();
var request = {address: 'Suisse Romande, Avenue de la Gare, Sion, Svizzera'};
var callBack = function(geocoderResults, geocoderStatus) {
    if(geocoderStatus === 'OK') {
        var location = geocoderResults[0].geometry.location;
        var latLng = new google.maps.LatLng(location.lat(), location.lng());
        map.setCenter(latLng);
    }
}
geocoder.geocode(request,callBack);
于 2013-04-05T21:58:46.330 回答
1

我已经修改了 Arun V 答案,使其完全适用于我的示例。

再次感谢 Arun V:

var request = {address: 'Suisse Romande, Avenue de la Gare, Sion, Svizzera'};
var callBack = function(geocoderResults, geocoderStatus) {
    if(geocoderStatus === 'OK') {
        var location = geocoderResults[0].geometry.location;
        var latLng = new google.maps.LatLng(location.lat(), location.lng());
        //get current Id from document useful if you have more than one map
        //you can't use "this.gmap" reference here because it's not in your scope
        var idMapW = this.document.activeElement.id;
        Ext.ComponentQuery.query('#'+idMapW)[0].down('gmappanel').gmap.setCenter(latLng);
        }
    };
    this.up('gmapw').down('gmappanel').geocoder.geocode(request,callBack);
于 2013-04-08T07:57:07.887 回答