我有一个天气图片我想用作谷歌地图android api中的叠加层。我希望获得与在KML文件中使用GroundOverlay相同的结果,例如
<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个纬度/长点,无论滚动,缩放等。
有没有办法使用为Android提供的谷歌api /地图来实现这一目标?
答案 0 :(得分:0)
你可以用叠加来做,但是你必须在叠加的绘画方法中做你自己的所有绘画。这可能是一个很好的延伸。
答案 1 :(得分:0)
以下是我的绘画方式。
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。 **
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;
}