0

I am using Google Maps API v2 for displaying the maps in android.

Is it possible to place temperature labels under each city name like what shown in below image.

Example

a.badawi
  • 13
  • 2
  • 3

1 Answers1

0

There is no way to get precise screen coordinates of cities names so you can't place temperature labels under each city name accurately. But you can hide all cities/towns names (and other labels) from map

Map with cities names

(please take a look at Add a Styled Map for more details) and get cities coordinates and names from databases like this or that and show city name and additional text as you wish.

To create map without cities names you can useStyled map wizard interactive mode (move Labels seek bar to the left)

Map without cities names

and than click "Finish" button and copy-paste JSON like this:

[
  {
    "elementType": "labels",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "administrative.land_parcel",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "featureType": "administrative.neighborhood",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  }
]

into file in your project e.g. src\main\res\raw\no_city_names_map_style.json. Then you can apply this style to your map this way:

mGoogleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getApplicationContext(), R.raw.no_city_names_map_style));

For placing text you can use Markers with custom dynamically created icon that contains only text, e.g. like in this answer of user2808624:

public BitmapDescriptor createPureTextIcon(String text) {

    Paint textPaint = new Paint(); // Adapt to your needs

    float textWidth = textPaint.measureText(text);
    float textHeight = textPaint.getTextSize();
    int width = (int) (textWidth);
    int height = (int) (textHeight);

    Bitmap image = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(image);

    canvas.translate(0, height);

    // For development only:
    // Set a background in order to see the
    // full size and positioning of the bitmap.
    // Remove that for a fully transparent icon.
    canvas.drawColor(Color.LTGRAY);

    canvas.drawText(text, 0, 0, textPaint);
    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(image);
    return icon;
}

Then you can place marker with custom text on map this way:

mMap.addMarker(new MarkerOptions()
    .position(<city_location>)
    .icon(BitmapDescriptorFactory.fromBitmap(createPureTextIcon("Name Temperature")))
    .anchor(0.5f, 1));
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79