开发者

Does android maps support ground overlay?

开发者 https://www.devze.com 2023-01-02 13:52 出处:网络
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

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.7开发者_如何学JAVA5</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?


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消