2014-04-11 62 views
0

我已經使用Gmaps.js創建地圖,並通過地點附近的地方,但它顯示黃頁列表,當我點擊下面的圖片解釋了問題。我是Google Maps API的新成員。Gmaps地圖顯示黃頁的房源

enter image description here

var map; 
var loc; 
    $(document).ready(function(){ 
     map = new GMaps({ 
     el: '#map', 
     lat: -12.043333, 
     lng: -77.028333, 
     zoom:15, 
     }); 

     GMaps.geocode({ 
      address: $('#address').val().trim(), 
      callback: function(results, status){ 
      if(status=='OK'){ 
       var latlng = results[0].geometry.location; 
       lat=latlng.lat(); 
      lng=latlng.lng();   
    map.setCenter(latlng.lat(), latlng.lng()); 
       map.addMarker({ 
       lat: latlng.lat(), 
       lng: latlng.lng(), 
     icon:$('#mainPin').val().trim(), 
       }); 

loc=new google.maps.LatLng(latlng.lat(),latlng.lng()); 
}}}); 
}); 
function showplaces(place,urll){ 
map.removeMarkers(); 
GMaps.geocode({ 
      address: $('#address').val().trim(), 
      callback: function(results, status){ 
      if(status=='OK'){ 
       var latlng = results[0].geometry.location; 
       map.setCenter(latlng.lat(), latlng.lng()); 
       map.addMarker({ 
       lat: latlng.lat(), 
       lng: latlng.lng(), 
     icon:$('#mainPin').val().trim(), 
       }); 
     map.addLayer('places', { 
      location : loc, 
      radius : 500, 
      types : [place], 
      search: function (results, status) { 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
       for (var i = 0; i < results.length; i++) { 
       var place = results[i]; 
       map.addMarker({ 
        lat: place.geometry.location.lat(), 
        lng: place.geometry.location.lng(), 
        title : place.name, 
        icon:urll, 

       }); 

       } 
      } 
      } 
     }); 

     } 
      } 
     }); 

if(place=='shopping_mall') 
    { 
    GMaps.geocode({ 
      address: $('#address').val().trim(), 
      callback: function(results, status){ 
      if(status=='OK'){ 
       var latlng = results[0].geometry.location; 
       map.setCenter(latlng.lat(), latlng.lng()); 
       map.addMarker({ 
       lat: latlng.lat(), 
       lng: latlng.lng(), 
     icon:$('#mainPin').val().trim(), 
       }); 
     map.addLayer('places', { 
      location : loc, 
      radius : 500, 
      types : ['shoe_store','store','department_store'], 
      search: function (results, status) { 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
       for (var i = 0; i < results.length; i++) { 
       var place = results[i]; 
       map.addMarker({ 
        lat: place.geometry.location.lat(), 
        lng: place.geometry.location.lng(), 
        title : place.name, 
        icon:urll, 

       }); 
       } 
      } 
      } 
     }); 

     } 
      } 
     }); 



    } 



else if(place=='movie_theater') 
    { 
    GMaps.geocode({ 
      address: $('#address').val().trim(), 
      callback: function(results, status){ 
      if(status=='OK'){ 
       var latlng = results[0].geometry.location; 
       map.setCenter(latlng.lat(), latlng.lng()); 
       map.addMarker({ 
       lat: latlng.lat(), 
       lng: latlng.lng(), 
     icon:$('#mainPin').val().trim(), 
       }); 
     map.addLayer('places', { 
      location :loc, 
      radius : 500, 
      types : ['movie_theater','zoo','stadium','night_club'], 
      search: function (results, status) { 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
       for (var i = 0; i < results.length; i++) { 
       var place = results[i]; 
       map.addMarker({ 
        lat: place.geometry.location.lat(), 
        lng: place.geometry.location.lng(), 
        title : place.name, 
        icon:urll, 

       }); 
       } 
      } 
      } 
     }); 

     } 
      } 
     }); 

    } 


} 

回答

1

你看到的有歸屬的,它需要向他們展示。

當然不需要多次顯示它們。

問題是(您可能會稱之爲錯誤,我稱之爲糟糕的實現......天知道爲什麼這麼多人使用這些第三方地圖庫,它只引入錯誤而不是有用的功能)。

「bug」:每次您撥打map.addLayer('places')時,都會創建一個新的google.maps.places.PlacesService實例。每個實例將打印單獨的歸因。

Gmaps.js沒有選項:

  • 定義,其中歸屬將被放置
  • 使用placesService

我的建議的單個實例將放棄這個「圖書館」。

當你不能沒有它時檢查showplaces的開頭是否有對象map.singleLayers.places。當它的時候它是placesService-instance。將此實例用於nearbySearch而不是使用map.addLayer('places')

+0

謝謝我已經習慣了google maps api,它比Gmap.js更好 –