2017-07-19 59 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请求非常快。但也许这就是它的工作原理。 –

相关问题