0

I am new to the Google Earth Engine javascript api. Sorry if my question is silly. I have included ee_api_js.js file from the git library of Google Earth Engine javascript api. I have validated the user with the ee.data.authenticate method successfully. Now when I am trying to add layer to map it gives me "Uncaught TypeError: Map.addLayer is not a function"

var map, ee;

function initMap() {
      var latlng = {lat: 31.046051, lng: 34.851612};
      map = new google.maps.Map(document.getElementById('map'), {zoom: 8, center: latlng, disableDefaultUI: true});
    }

var runAnalysis = function() {
            ee.initialize();
var image = ee.Image('COPERNICUS/S2/20180521T081559_20180521T082244_T36SYB'); 
            var visParams_ndvi = {min: 0.1, max: 0.9, palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
                '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'};
            var image_ndvi = image.normalizedDifference(['B8','B4']);
            map.addLayer(image_ndvi,visParams_ndvi,'NDVI');
    };


    $(document).ready(function(){
        //$('#date').flatpickr();

        // Shows a button prompting the user to log in.
        var onImmediateFailed = function() {
            ee.data.authenticateViaPopup(function() {
                // If the login succeeds, hide the login button and run the analysis.
                //$('.g-sign-in').addClass('hidden');
                runAnalysis();
              });
        };

        // Attempt to authenticate using existing credentials.
        ee.data.authenticate(CLIENT_ID, runAnalysis, null, null, onImmediateFailed);
    });
0

Your map is not a ui.Map from the EE library. It's a google.maps.Map which is different. This is a detailed example showing how to call getMap() on your EE object and passing the token and id to the maps API.

  • Thanks, what's the best way so that i can use the same code of google earth engine console using javascript api, I don't want to go with python. – Nitin Muchhadiya Jul 7 '18 at 7:30
  • Is there any documentation available for ee_api_js.js which shows how to use their functions? – Nitin Muchhadiya Jul 7 '18 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.