简体   繁体   English

Sencha Touch 2删除Google地图标记

[英]Sencha Touch 2 remove Google map Markers

i'm trying to remove all the markers from a google map using ExtJS. 我正在尝试使用ExtJS从Google地图中删除所有标记。 I'm performing a setCenter() operation (i need to remove the old center from the map) and also i want to add a new marker in the new center. 我正在执行setCenter()操作(我需要从地图上删除旧的中心),而且我想在新的中心添加新的标记。

As i know, to get the google map instance i need to perform a getMap() over the map container, ie map.getMap() 据我所知,要获取谷歌地图实例,我需要在地图容器上执行getMap(),即map.getMap()

I tried with clearMarkers();, deleteMarkers(); 我尝试过clearMarkers();、 deleteMarkers(); and markers = []; 和标记= []; but neither works. 但都行不通。 The google map object looks really weird on the Chrome Developer tools utility, at least for me. 谷歌地图对象在Chrome开发者工具实用程序上看起来很奇怪,至少对我而言。 这里是快照

With the addition, same problem. 另外,同样的问题。 I'm doing that: 我正在这样做:

new google.maps.Marker({
                            map       : map.getMap(),
                            position  : new google.maps.LatLng(location.lat, location.lng),
                            title     : 'Drag Marker To New Position',
                            animation : google.maps.Animation.DROP,
                            draggable : true
});

Any help is appreciated! 任何帮助表示赞赏! Thanks. 谢谢。

It's very simple. 非常简单 To remove a marker from the map, call the setMap() method passing null as the argument. 要从地图中删除标记,请调用setMap()方法,并将null用作参数。

marker.setMap(null);

Note that the above method does not delete the marker. 请注意 ,上述方法不会删除标记。 It simply removes the marker from the map. 它只是从地图上删除标记。 If instead you wish to delete the marker, you should remove it from the map, and then set the marker itself to null . 相反,如果您希望删除标记,则应将其从地图上删除,然后将标记本身设置为null

If you wish to manage a set of markers, you should create an array to hold the markers. 如果要管理一组标记,则应创建一个数组来保存标记。 Using this array, you can then call setMap() on each marker in the array in turn when you need to remove the markers . 使用此数组, 然后可以在需要删除标记时依次对数组中的每个标记调用setMap() You can delete the markers by removing them from the map and then setting the array's length to 0, which removes all references to the markers. 您可以删除标记,方法是将它们从地图中删除,然后将数组的长度设置为0,这将删除所有对标记的引用。

Find an example here View example (marker-remove.html) 在此处查找示例查看示例(marker-remove.html)

Read more about Google Maps Tutorial - Remove a marker 阅读有关Google Maps Tutorial的更多信息-删除标记

Finally i get a solution for that (maybe it's not the better way, but it works) 最终,我得到了一个解决方案(也许这不是更好的方法,但是有效)

I decided to have a global variable that references the marker that i have to put over the map. 我决定有一个全局变量,该变量引用必须放置在地图上的标记。

To define a global variable in sencha i use this approach: 要在sencha中定义全局变量,请使用以下方法:

Ext.application({
    name: 'SIGCC',
    marker: null,
    ....

    ....

});

I'm using a custom class to get an AutocompleteTextField, and when the user taps a suggested location the previous marker has to be removed and a new one is placed over the google map. 我正在使用一个自定义类来获取AutocompleteTextField,并且当用户点击建议的位置时,必须删除之前的标记并将新的标记放置在Google地图上。 Also i have to recenter the map in the new location 我也必须在新位置更新地图

recenterMap: function(location){
    //addressMap is the id of the xtype map component
    var map = Ext.getCmp('addressMap');
    map.setMapCenter({ latitude: location.lat, longitude: location.lng });
    if (SIGCC.app.marker){
      SIGCC.app.marker.setMap(null);
    }
    SIGCC.app.marker = new google.maps.Marker({
                                map       : map.getMap(),
                                position  : new google.maps.LatLng(location.lat, location.lng),
                                title     : 'Drag Marker To New Position',
                                animation : google.maps.Animation.DROP,
                                draggable : true
    });
    //My map is hidden before de users tap action, so i have to unhide them
    map.setHidden(false);

  },

As you can see, this portion of code references the global variable defined previously. 如您所见,这部分代码引用了先前定义的全局变量。 Hope this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM