【问题标题】:Google Place API v3 with Sencha Touch 2.x带有 Sencha Touch 2.x 的 Google Place API v3
【发布时间】:2013-03-14 13:44:24
【问题描述】:

我是 Sencha Touch 的新手,了解 Javascript、jQuery、HTML5。

我正在使用 Sencha Touch + Sencha Architect 开发简单的 Google Map 应用程序,它在当前位置放置标记,还使用 ​​Google Place 查找当前位置附近的“商店”(即需要 Google 地点搜索的类型)。我在“callbackPlaces”中获取位置(即纬度、经度),但 Google.Maps.Marker 在“callbackPlaces”方法中给出错误(即通过“addMarker”方法调用,我还尝试通过将 Google.Maps.Marker 放入'callbackPlaces')。

Ext.define('MyApp.view.Map', {
    extend: 'Ext.Map',
    alias: 'widget.map',

    config: {
        useCurrentLocation: true,
        listeners: [
            {
                fn: 'onMapMaprender',
                event: 'maprender'
            }
        ]
    },

    onMapMaprender: function(map, gmap, options) {
        this.initializePlaces();
    },

    initializePlaces: function() {

        //Set Marker on Current Position
        var geo = this.getGeo();
        var currentPosition = new google.maps.LatLng(geo.getLatitude(), geo.getLongitude());
        this.createMarker(currentPosition);

        //request set for Places Services
        var request = {
            location: currentPosition,
            radius: 500,
            types: ['store']
        };


        this.getMap().zoom = 15;

        // Call PlacesService
        var service = new google.maps.places.PlacesService(this.getMap());
        service.nearbySearch(request, this.callbackPlaces); 



    },

    callbackPlaces: function(results, status) {

        if (status == google.maps.places.PlacesServiceStatus.OK) {  

            for (var i = 0; i < results.length; i++) {
                addMarker(results[i]);
            }
        }
    },

    createMarker: function(position) {
        //Here Position = Google Map LatLng

        var infoWindow = new google.maps.InfoWindow();

        var marker = new google.maps.Marker({
            map: this.getMap(),
            position: position
        });

        google.maps.event.addListener(marker, "click", function() {    
            infoWindow.setContent("Lng: " + marker.position.lng() + "\n Lat: " + marker.position.lat());
            infoWindow.open(this.getMap(), marker);
        }); 
    },

    addMarker: function() {
        //Here Place = google.maps.places

        var infoWindow = new google.maps.InfoWindow();

        var marker = new google.maps.Marker({
            map: this.getMap(),
            position: place.geometry.location
        });


        google.maps.event.addListener(marker, "click", function() {    
            infoWindow.setContent("Lng: " + marker.position.lng() + "\n Lat: " + marker.position.lat());
            infoWindow.open(this.getMap(), marker);
        }); 
    }

});

我不知道出了什么问题!

任何帮助|提示将不胜感激!

提前致谢。

【问题讨论】:

  • 你不应该叫 this.addMarker(results[i]);而不是 addMarker(results[i]); ?
  • 实际上同样的事情是在“initializePlaces”中工作,也试过没有“this”但它不起作用。
  • 事情是......我无法在“callbackPlaces”方法中使用“this”获得任何东西。除了“callbackPlaces”之外,所有方法都可以访问“this”。所以,我在“callbackPlaces”方法中尝试使用和不使用“this”。但无论哪种方式都行不通。很奇怪!
  • 在'callbackPlaces'中'this'是'[object Window]',而在其他地方'this'是'[object Object]'

标签: google-maps sencha-touch-2 google-places-api sencha-architect


【解决方案1】:

您指出 callbackPlaces 函数中存在范围问题。

因此您应该替换以下行:

service.nearbySearch(request, this.callbackPlaces); 

service.nearbySearch(request, function (results, status) {
  this.callbackPlaces.call(this, [results, status]);
}); 

希望对你有帮助

【讨论】:

  • 它给出错误:“无法调用'undefined'的方法'call'。我在“函数(结果,状态){}”之间写的任何东西都可以使用,除了'this'。它认为'this' = [Object Global] 而不是 [object Map] 或 [object Object]
  • 我是否遗漏了设置上的任何内容,这需要通过 Google API 完成才能与 Sencha Touch 配合使用。顺便说一句,我正在使用 Sencha Touch 2.1 和 Sencha Architect
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多