简体   繁体   中英

Specify API Key For Google Matrix API Javascript To Retrieve Usage

i am using google matrix API successfully to retrieve the time and distance for location a to location b.

var origins = [Task.getItem("Y") + "," + Task.getItem("X")];
            var destinations = [LAT + "," + LNG];

            var distanceMatrix = new google.maps.DistanceMatrixService();
            var distanceRequest = {
                origins: origins,
                destinations: destinations,
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            };
            distanceMatrix.getDistanceMatrix(distanceRequest, function(response, status) {
                if (status != google.maps.DistanceMatrixStatus.OK) {
                    alert('Error was: ' + status);
                } else {
                    var origins = response.originAddresses;
                    var destinations = response.destinationAddresses;
                    //debugger;
                    if (response.rows[0].elements[0].distance != null) {
                        totalDistance = response.rows[0].elements[0].distance.value;
                        totalTime = response.rows[0].elements[0].duration.value;
                        ratioPerOneMeter = totalDistance / totalTime;
                        PRDifference = 0;

                        updateDistanceAndTime();

                    } else {
                        alert("The Distance And Time Cannot Be Calculated");
                    }
                }
            });

What I want to do is add the API key but I don't know how. I did add in the API key:

<script src="http://maps.googleapis.com/maps/api/js?key={KEY}&v=3.exp">

as stated here: Using a Google Apps API key with Distance Matrix

but I think this is just for the map and not for the matrix API. Can someone inform me if this is enough.

The Google Maps Distance Matrix Service in the Google Maps Javascript API uses the key provided in the API include.

<script src="http://maps.googleapis.com/maps/api/js?key={KEY}">

That usage is not tracked separately in the developer console. The usage tracked under "Google Maps Distance Matrix API" is the usage of the Web Service only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM