0

我是Sencha Touch的新手,懂Javascript,jQuery,HTML5。Sencha Touch 2.x Google Place API v3

我正在用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); 
     }); 
    } 

}); 

我不知道出了什麼問題!

任何幫助|提示將不勝感激!

在此先感謝。

+1

你不應該叫this.addMarker(結果[1]);而不是addMarker(results [i]); ? – 2013-03-14 14:45:11

+0

實際上同樣的事情是'initializePlaces'中的工作也嘗試沒有'this',它不工作。 – 2013-03-15 05:02:21

+0

事情是......我不能在'callbackPlaces'方法中得到'this'的任何東西。除了「callbackPlaces」之外,所有方法都可以訪問「this」。所以,我在'callbackPlaces'方法中嘗試使用和不使用'this'。但不是以任何方式工作。很奇怪! – 2013-03-15 10:05:43

回答

1

您指出callbackPlaces函數中存在範圍問題。

因此,你應該替換下面的行:

service.nearbySearch(request, this.callbackPlaces); 

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

希望這有助於

+0

它給出錯誤:「不能調用'undefined'的方法'調用',我在中間寫的東西」函數(結果,狀態){}「工作,除了'this'。它認爲'this'= [對象全局]而不是[對象映射]或[對象對象] – 2013-03-15 11:42:12

+0

我錯過任何設置,需要完成使用Google API與Sencha Touch一起工作順便說一句,我正在使用Sencha Touch 2.1和Sencha Architect – 2013-03-15 11:45:25