难道Android地图支持地面叠加? [英] Does android maps support ground overlay?

查看:164
本文介绍了难道Android地图支持地面叠加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个天气形象,我想在谷歌的叠加使用Android的地图API。我想实现我从KML文件,如

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>

以上将确保4个角我的形象留固定在4纬度/经度点上市,无论滚动,缩放等。

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..

有没有办法使用谷歌API来完成这项/提供Android地图?

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

推荐答案

下面是我怎么做的图纸。

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

数据类:

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());
    }

}

下面是我如何解析JSON。 **注意:我的JSON是从上面的例子有点不同。我收拾JSON我的服务器上的第一。 **

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

这篇关于难道Android地图支持地面叠加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆