4

I have a weather image I would like to use as an overlay in the google maps android api. I would like to achieve the same result that I get from using GroundOverlay in KML files, such as

<GroundOverlay>  
  <name>myimage</name>  
  <Icon>
    <href>myimage.png</href>
    <viewBoundScale>0.75</viewBoundScale>
  </Icon>
  <LatLonBox>
    <north>75.6088</north>
    <south>5.0121</south>
    <east>182.2805</east>
    <west>120.6795</west>
  </LatLonBox>
</GroundOverlay>

The above will ensure that the 4 corners of my image stay anchored to the 4 lat/long points listed, regardless of scrolling, zooming etc..

Is there a way to accomplish this using the google api/maps provided for android?

wmh1108
  • 71
  • 3
  • 1
    Have you ever solved this? We are getting calls to port our iOS app to Android, and it relies on tiled overlays. I'm not sure what the equivalent best way in Android is. – radven Dec 25 '10 at 19:54
  • Not sure when was the support for GroundOverlays added into Google Maps Android API but it is available now. The API has a class called 'GroundOverlay' that does exactly what the equivalent javascript API do. Here is the [link](http://developer.android.com/reference/com/google/android/gms/maps/model/GroundOverlay.html). – Nouman Hanif Nov 13 '14 at 06:20

2 Answers2

0

You can do it with an overlay but you would have to do all your own painting in the paint method of Overlay. That could be a good extension though.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
0

Here is how I am doing the drawing.

public class GroundOverlay extends Overlay {

    private GroundOverlayData data = null;

    private final int strokeWidth = 1;
    private Paint borderPaint = new Paint();
    private Paint bitmapPaint = new Paint();

    public GroundOverlay(GroundOverlayData data) {
        super();
        this.data = data;

        bitmapPaint.setAlpha(100);

        borderPaint.setStrokeWidth(strokeWidth);
        borderPaint.setColor(Color.BLACK);
        borderPaint.setAlpha(20);
        borderPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);

        if (data != null) {
            Point northWest = toPoint(mapView.getProjection(), data.getNorthWestCoordinate().getGeoPoint());
            Point southEast = toPoint(mapView.getProjection(), data.getSouthEastCoordinate().getGeoPoint());

            Rect bitmapRect = new Rect(northWest.x, northWest.y, southEast.x, southEast.y);
            if (data.getBitmap() != null) {

                if (!data.getBitmap().isRecycled()) {
                    canvas.drawBitmap(data.getBitmap(), null, bitmapRect, bitmapPaint);
                }
            }

            //Border
            Rect borderRect = new Rect(bitmapRect.left-strokeWidth, bitmapRect.top-strokeWidth, 
                    bitmapRect.right+strokeWidth, bitmapRect.bottom+strokeWidth);
            canvas.drawRect(borderRect, borderPaint);
        }
    }

    private Point toPoint(Projection projection, GeoPoint geoPoint) {
        Point point = new Point();
        projection.toPixels(geoPoint, point);
        return point;
    }

    public GroundOverlayData getData() {
        return data;
    }

    public void setData(GroundOverlayData data) {
        this.data = data;
    }
}

Data classes:

public class GroundOverlayData {

    private Bitmap bitmap = null;
    private Coordinate northWestCoordinate = null;
    private Coordinate southEastCoordinate = null;


    public Bitmap getBitmap() {
        return bitmap;
    }
    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }
    public Coordinate getNorthWestCoordinate() {
        return northWestCoordinate;
    }
    public void setNorthWestCoordinate(Coordinate northWestCoordinate) {
        this.northWestCoordinate = northWestCoordinate;
    }
    public Coordinate getSouthEastCoordinate() {
        return southEastCoordinate;
    }
    public void setSouthEastCoordinate(Coordinate southEastCoordinate) {
        this.southEastCoordinate = southEastCoordinate;
    }


}

public class Coordinate implements Serializable {

    private static final long serialVersionUID = -2779462973231193512L;

    private transient GeoPoint geoPoint = null;

    public Coordinate() {

    }

    public Coordinate(Double latitude, Double longitude) {
        this.geoPoint = Coordinate.toGeoPoint(latitude, longitude);
    }

    public GeoPoint getGeoPoint() {
        return geoPoint;
    }

    public void setGeoPoint(GeoPoint geoPoint) {
        this.geoPoint = geoPoint;
    }
    public void setLatLong(Double latitude, Double longitude) {
        setGeoPoint(Coordinate.toGeoPoint(latitude, longitude));
    }


    public static GeoPoint toGeoPoint(Double latitude, Double longitude) {

        Double lon = longitude * 1E6;
        Double lat = latitude * 1E6;

        return new GeoPoint(lat.intValue(), lon.intValue());
    }

}

Here is how I parse the JSON. ** Note my JSON is a little different from the example above. I clean up the JSON on my server first. **

private static List<GroundOverlayData> parseGroundOverlays(String json) throws JSONException {

    JSONArray overlaysArray = new JSONArray(json);
    List<GroundOverlayData> groundOverlaysData = new ArrayList<GroundOverlayData>();

    for (int i = 0 ; i < overlaysArray.length() ; i++) {
        JSONObject overlayObj = (JSONObject) overlaysArray.get(i);
        GroundOverlayData data = new GroundOverlayData();
        data.setBitmap(getBitmapFromUrl(overlayObj.getString("imageUrl")));

        data.setNorthWestCoordinate(new Coordinate(Double.valueOf(overlayObj.getString("north")), 
                Double.valueOf(overlayObj.getString("west"))));
        data.setSouthEastCoordinate(new Coordinate(Double.valueOf(overlayObj.getString("south")), 
                Double.valueOf(overlayObj.getString("east"))));
        groundOverlaysData.add(data);
    }

    return groundOverlaysData;
}


public static Bitmap getBitmapFromUrl(String url) throws MalformedURLException, IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-agent","Mozilla/4.0");

    connection.connect();
    InputStream input = connection.getInputStream();

    Bitmap output = null;
    try {
        output = BitmapFactory.decodeStream(input);
    } catch (Throwable e) {

    } finally {
        try {
            input.close();
        } catch (Exception e) {}
    }
    return output;
}
Randy Findley
  • 329
  • 2
  • 6