1

I assume this is a really basic question but I can't find the solution anywhere. I simply want to add some text onto a map layer at a certain lat/lon location.

2

Well you can't really add labels in map as overlays per se but there is definitely some roundabout way where you can add texts that you want to the map layer. What this approach does is embed the text onto the map layer itself.

This actually uses a package created by GEE user gena. I actually found how to do this from @shahryar who answered this stack question who also found it on GEE developers group so credits to them.

How you use this package is by first importing it

var text = require('users/gena/packages:text');

I will use SRTM and my own random geometry for example but this should work with any Image layer.

var srtm = ee.Image("CGIAR/SRTM90_V4"),
    geometry = ee.Geometry.Point([84.76194615096051, 28.371696851094043]);

Well then you can set up the way your text appears. I don't have a lot of info on this package so I am unaware of any additional options. The name you provide in property is what will contain your text in the image. scale controls the size of text which can change based on zoom level of map WHEN RUN so keep that in mind.

var annotations = [
  {position: 'left', offset: '1%', margin: '1%', property: 'label', scale: Map.getScale() * 2}
]

Next thing to do is to change the map layer to RGB mode as the package won't work with single bands (for now). Even if you have multiple bands, you want to visualize it in RGB. Here we also, add the text we want as label.

var image = srtm.visualize({
    forceRgbOutput: true,
    min: 0,
    max: 5000
  }).set({label:'test_text'});

Next we just get a new image object annotated where we add the pre-mentioned annotation/text to the image. You can specify a polygon as geometry as well which will use that position property of annotations to place your text.

var annotated = text.annotateImage(image, {}, geometry, annotations);

Map.addLayer(annotated)

This is it, you have the text you wanted on your map. Its def not as dynamic as a label of annotation in GIS but it should work if you know which scale you are working on. The only trade-off is that since the layers are prepared when the application is RUN, you either need to specify a fixed size for text of hit run every time you want to readjust the text size.

You can see a working link here

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.