android-maps-utils icon indicating copy to clipboard operation
android-maps-utils copied to clipboard

Kml Overlay Display Placemark names without bubbles

Open owuor91 opened this issue 8 years ago • 10 comments

Summary: I want to overlay a kml file on a map and I expect names of placemarks to be displayed without bubbles

Steps to reproduce:

Overlay a kml file with placemarks and points and names onto a map

Expected behavior:

Display the names of the places without the red bubbles

Observed behavior:

The kml file is rendered with bubbles at the points where names ought to be displayed, the names are only displayed when the bubbles are clicked

Device and Android version: Tecno RA6 (Android 8.1.0 Oreo) stock ROM,

OnePlus 5(Android 8.1.0 oreo) stock ROM

Screenshots:

screenshot_20180712-164558 -Tecno RA6

screenshot_20180712-172056 -Oneplus 5

This is the same kml file rendered on google earth (names in yellow) screen shot 2018-07-12 at 17 31 33

owuor91 avatar Jul 12 '18 14:07 owuor91

can you give me the sample code for linestring display in map ?

Zephyr2627 avatar Sep 08 '18 15:09 Zephyr2627

Hi @Zephyr2627 Here is the sample code

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
  }

@Override
  public void onMapReady(GoogleMap ggleMap) {
    this.googleMap = ggleMap;
 try {
      KmlLayer kmlLayer = new KmlLayer(googleMap, R.raw.northarea, getApplicationContext());
      kmlLayer.addLayerToMap();
  } catch (Exception e) {
      e.printStackTrace();
  }

}

owuor91 avatar Sep 11 '18 10:09 owuor91

Here is the kml file. Please rename extension to .kml northarea.txt

owuor91 avatar Sep 11 '18 10:09 owuor91

any solution you for this

VeenaHosur avatar Sep 16 '19 05:09 VeenaHosur

I used a hack. I extracted the points and names from the kml file and served them to the app as json. I then overlayed the KML file to only show the polygons. Afterwards I plotted the names onto the map as markers

owuor91 avatar Sep 16 '19 08:09 owuor91

if you have sample code with screen shot can you share it Please

VeenaHosur avatar Sep 17 '19 04:09 VeenaHosur

This is the code for plotting the names as markers on the map

public void displayLabels(GoogleMap googleMap, List<KmlPoint> kmlPoints) {
    markerList = new ArrayList<Marker>();

    for (KmlPoint kmlPoint : kmlPoints) {
      Bitmap txtBitmap = makeBitmap(context, kmlPoint.getName());
      if (kmlPoint.getCoordinates() != null) {
        String[] coordinatesArray = kmlPoint.getCoordinates().split(Constants.COMMA);
        String latitude = coordinatesArray[0];
        latitude = latitude.replace(Constants.DEGREE_SYMBOL, Constants.EMPTY_STRING).trim();
        String longitude = coordinatesArray[1];
        longitude = longitude.replace(Constants.DEGREE_SYMBOL, Constants.EMPTY_STRING).trim();
        MarkerOptions markerOptions =
            new MarkerOptions().position(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)))
                .icon(BitmapDescriptorFactory.fromBitmap(txtBitmap));

        Marker marker = googleMap.addMarker(markerOptions);
        markerList.add(marker);
      }
    }
  }

owuor91 avatar Sep 17 '19 12:09 owuor91

KmlPoint is a simple POJO like this

public class KmlPoint {
  private String name;

  private String coordinates;

  private KmlPoint(Builder builder) {
    setName(builder.name);
    setCoordinates(builder.coordinates);
  }

  public static Builder newBuilder() {
    return new Builder();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getCoordinates() {
    return coordinates;
  }

  public void setCoordinates(String coordinates) {
    this.coordinates = coordinates;
  }

  public static final class Builder {
    private String name;
    private String coordinates;

    private Builder() {
    }

    public Builder withName(String val) {
      name = val;
      return this;
    }

    public Builder withCoordinates(String val) {
      coordinates = val;
      return this;
    }

    public KmlPoint build() {
      return new KmlPoint(this);
    }
  }
}

name is the area name, coordinates are the coordinates for the point where the name should be plotted. Each name is plotted on a transparent bitmap using this create bitmap method.

public Bitmap makeBitmap(Context context, String text) {
    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Bitmap bitmap = Bitmap.createBitmap(text.length() * 25, 50, ARGB_8888);
    bitmap = bitmap.copy(ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.LTGRAY);
    paint.setColor(Color.BLUE);
    paint.setTextSize(12 * scale);
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, text.length(), rect);
    int x = bitmap.getWidth() - rect.width();
    int y = rect.height();
    canvas.drawText(text, 0, y, paint);

    return bitmap;
  }

owuor91 avatar Sep 17 '19 12:09 owuor91

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

stale[bot] avatar Dec 16 '19 13:12 stale[bot]

The issue with this KML isn't with the name rendering. If you upload the same KML to Google Maps, it will also only display the names in an info window when clicked. There is an issue with the star icon not displaying though, which is why the default red bubble marker icons are displayed instead.

First of all, the bitmap isn't downloading. This is because the URL is http and not https, which is required by default in Android 9+.

But even after changing the URL to https, there is an issue with the icon not getting added to the markers because the code only checks for "Point" geometry types, and the geometry for the KmlPlacemarks is "MultiGeometry". It needs to recursively iterate over these multi-geometry KmlPlacemarks and check for "Point" sub-geometries.

I fixed this in https://github.com/googlemaps/android-maps-utils/pull/605 and confirmed the KML linked above renders correctly with the URL changed to https.

jeffdgr8 avatar Jan 26 '20 10:01 jeffdgr8