Google Maps: ReferenceError: google is not defined in cordova android application

允我心安 提交于 2020-01-25 06:47:25

问题


I'm trying to create a cordova project that marks some locations in map and getting the distance in between the selected location and current location of the user.

I have used cordova-plugin-googlemaps for displaying the google map and used cordova-plugin-googlemaps and for getting the distance netween the selected location and current location of the user I have used the code from google map distance matrix service

I am getting ReferenceError: google is not defined when I run my cordova application in android device but its working fine in browser

I defined google map like this>

......
......

plugin.google.maps.environment.setEnv({
   'API_KEY_FOR_BROWSER_RELEASE': config.GOOGLE_MAP_API_KEY,
   'API_KEY_FOR_BROWSER_DEBUG': config.GOOGLE_MAP_API_KEY
});
initializeGMap(currentLat, currentLng);

......
......


map distance matrix service


......
......

function showNearestROs() {
        mapData.forEach(roData => {
            var coordinates = roData.coordinates.substring(1, roData.coordinates.length).substring(0, roData.coordinates.length - 2).split(',');
            var destination = {
                lat: parseFloat(coordinates[0].trim()),
                lng: parseFloat(coordinates[1].trim()),
            };
            origin = {
                lat: parseFloat(localStorage.getItem("currentLat").trim()),
                lng: parseFloat(localStorage.getItem("currentLng").trim()),
            };
            if (roData.status === 1) {
                calculateRoadMapDistance(origin, destination, roData);
            } else {
                console.log("Charging station unavailable");
            }
        });
    }

    function calculateRoadMapDistance(origin, destination, roData) {

        var service = new google.maps.DistanceMatrixService();

        service.getDistanceMatrix({
            origins: [origin],
            destinations: [destination],
            travelMode: 'DRIVING',
            unitSystem: google.maps.UnitSystem.METRIC,
            // unitSystem: google.maps.UnitSystem.IMPERIAL,

        }, function (response, status) {
            if (status !== 'OK') {
                console.log('Error was: ' + status);
            } else {

                var travelInfo = response.rows[0].elements[0]

                if (travelInfo.status == 'OK') {
                    roMap.push({
                        'distance': parseFloat(travelInfo.distance.text.split(" ")[0].replace(/,/g, '')),
                        'duration': travelInfo.duration.text,
                        'data': roData,
                    });
                } else {
                    console.log("error = ", travelInfo.status);
                }
            }
        });
    }

but I am getting error at var service = new google.maps.DistanceMatrixService();

来源:https://stackoverflow.com/questions/58449871/google-maps-referenceerror-google-is-not-defined-in-cordova-android-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!