2017-07-19 61 views
-1

我在做一個使用Google API的應用程序。在玩了幾個小時後,我注意到我最終把搜索api放在了地方。對地方api web服務的許多請求,是否有可能以另一種方式做到這一點?

enter image description here

我希望能夠尋找一個地址,並把它添加到地圖中。我做錯了嗎?或者爲什麼我已經創建了這麼多的請求? 是否有可能在其他地方獲得地址?

我知道這可能很難說如果我做錯了,但只是從請求的角度來看,我認爲你可能會說我的代碼正在做出許多請求。

這是我使用的代碼:

  $scope.initiateMap = function() { 
       $scope.map = new google.maps.Map(document.getElementById('map'), { 
        zoom: 11 
       }); 
       //var marker = new google.maps.Marker({ 
       // position: uluru, 
       // map: map 
       //}); 

       navigator.geolocation.getCurrentPosition(function (position) { 
        var initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
        $scope.map.setCenter(initialLocation); 
       }, function() { 
        console.log('user denied request for position'); 
        }); 

       var input = document.getElementById('address-field'); 
       var searchBox = new google.maps.places.SearchBox(input); 
       //map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

       $scope.map.addListener('bounds_changed', function() { 
        searchBox.setBounds($scope.map.getBounds()); 
       }); 

       var markers = []; 
       searchBox.addListener('places_changed', function() { 
        var places = searchBox.getPlaces(); 
        //var address_autocomplete = new google.maps.places.Autocomplete(address_input, { types: ['address'] }); 
        //if (places.length == 0) { 
        // return; 
        //} 

        // Clear out the old markers. 
        markers.forEach(function (marker) { 
         marker.setMap(null); 
        }); 
        markers = []; 

        // For each place, get the icon, name and location. 
        var bounds = new google.maps.LatLngBounds(); 
        places.forEach(function (place) { 
         if (!place.geometry) { 
          console.log("Returned place contains no geometry"); 
          return; 
         } 
         if (place.types[0] !== "street_address") { 
          alert("not a street address"); 
          markers = []; 
          markers.forEach(function (marker) { 
           marker.setMap(null); 
          }); 
          return; 
         } 
         $scope.apartment.Address.Longitude = place.geometry.location.lng(); 
         $scope.apartment.Address.Latitude = place.geometry.location.lat(); 

         $scope.apartment.Address.AddressName = place.formatted_address; 
         var icon = { 
          url: place.icon, 
          size: new google.maps.Size(71, 71), 
          origin: new google.maps.Point(0, 0), 
          anchor: new google.maps.Point(17, 34), 
          scaledSize: new google.maps.Size(25, 25) 
         }; 

         // Create a marker for each place. 
         markers.push(new google.maps.Marker({ 
          map: $scope.map, 
          icon: icon, 
          title: place.name, 
          position: place.geometry.location 
         })); 

         if (place.geometry.viewport) { 
          // Only geocodes have viewport. 
          bounds.union(place.geometry.viewport); 
         } else { 
          bounds.extend(place.geometry.location); 
         } 
        }); 
        $scope.map.fitBounds(bounds); 
       }); 
      } 
+0

在您的開發者控制檯中,您可以看到您提出了多少請求,顯示代碼 –

+0

問題中的圖像來自該控制檯。或者你想要另一張圖片? –

+0

也許你可以將它設置爲150k這樣的請求:https://stackoverflow.com/questions/33055206/google-places-api-how-do-i-increase-the-free-quota-to-150-000 - 每天要求 –

回答

0

因爲它是在谷歌的導遊說,你可以增加請求限制免費的,最多每24小時15萬點的請求,通過啓用結算上Google API控制檯。這只是爲了驗證你的身份,所以你的卡不會被收費。

查看更多here

+0

偉大!我正在努力。它只是覺得1000請求非常快。但也許這就是它的工作原理。 –

相關問題