Google地图叠加图片不可见

时间:2011-03-11 08:38:51

标签: android maps overlay

我想在Google地图地图视图上叠加地图图片(.jpg)。 问题是:叠加图像没有显示,谁知道是什么 问题

logcat中没有错误,MapView正在运行。

代码:

package nl.ultimo.android.skikaart;

import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;

public class MapsActivity extends MapActivity
{
        Bitmap          bmp;    //Loaded bitmap in memory
    GeoPoint        min;    //Top left corner (lat/long)
    GeoPoint        max;    //Right bottom corner (lat/long)

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       float lat1 = 45.19775f;
       float lng1 = 6.28418333f;
       float lat2 = 45.2636667f;
       float lng2 = 6.17431667f;

       min = new GeoPoint((int)(lat1 * 1E6), (int)(lng1 * 1E6));               // bounding rectangle
       max = new GeoPoint((int)(lat2 * 1E6), (int)(lng2 * 1E6));

       MapView mapView = (MapView) findViewById(R.id.mapView);
       MapController ctrl = mapView.getController();

       int x = (min.getLongitudeE6() + max.getLongitudeE6())/ 2;  //Select map center
       int y = (min.getLatitudeE6() + max.getLatitudeE6())/ 2;

       ctrl.setCenter(new GeoPoint(y,x));
       ctrl.setZoom(12);                 //Set scale
       mapView.setBuiltInZoomControls(true); //Enable zoom controls

       //Add overlay
       MapOverlay mapOverlay = new MapOverlay();
       List<Overlay> listOfOverlays = mapView.getOverlays();
       listOfOverlays.clear();
       listOfOverlays.add(mapOverlay);

       mapView.invalidate();
   }

   class MapOverlay extends com.google.android.maps.Overlay
   {
       @Override
       public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
       {
           super.draw(canvas, mapView, shadow);

           //Translate the GeoPoint to screen pixels
           Point screenPts = new Point();
           mapView.getProjection().toPixels(min, screenPts);

           //The overlay image
           Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.skikaart);

           //Prepare two rectangles (pixels)
           Point top_left = new Point();
           mapView.getProjection().toPixels(min, top_left);
           Point bottom_right = new Point();
           mapView.getProjection().toPixels(max, bottom_right);

           Rect src = new Rect( 0,0,bmp.getWidth() - 1, bmp.getHeight() - 1 );
           Rect dst = new Rect( top_left.x, bottom_right.y,bottom_right.x,top_left.y );

           canvas.drawBitmap(bmp, src, dst, null);

           return true;
       }
   }


   @Override
   protected boolean isRouteDisplayed() {
       return false;
   }
}

7 个答案:

答案 0 :(得分:8)

查看下一步需要做的标记

Drawable drawable = ...set your marker drawable
    drawable.setBounds(-drawable.getIntrinsicWidth() / 2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() / 2, 0);
    overlayitem.setMarker(drawable);

答案 1 :(得分:3)

这里要明确的是引用

的引用
  

“必须调用setBounds(Rect)方法以告诉Drawable绘制它的位置以及它应该有多大。”

答案 2 :(得分:2)

您的代码有一个小错误。

更改

Rect dst = new Rect( top_left.x, bottom_right.y,bottom_right.x,top_left.y );

 Rect dst = new Rect( top_left.x, top_left.y,bottom_right.x,bottom_right.y );

你应该好。

答案 3 :(得分:1)

更改此代码

min = new GeoPoint((int)(lat1 * 1E6), (int)(lng2 * 1E6));  // bounding rectangle
max = new GeoPoint((int)(lat2 * 1E6), (int)(lng1 * 1E6));

而不是

min = new GeoPoint((int)(lat1 * 1E6), (int)(lng1 * 1E6));  // bounding rectangle
max = new GeoPoint((int)(lat2 * 1E6), (int)(lng2 * 1E6));

并尝试其工作。

答案 4 :(得分:1)

请记住,每次需要渲染叠加层时都会调用draw()。当用户移动地图时,这可能每秒发生多次!!

您需要仅加载资源ONCE,然后在绘图调用期间仅将位图绘制到画布上。

此外,您当前的实现与实际活动高度耦合。

在您的活动中

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ...

    map.getOverlays().add(new PNGMapOverlay(getResources(), R.drawable.bitmap_id));

}


// allows re-factoring into separate class/file
class MapOverlay extends com.google.android.maps.Overlay
{
    private Bitmap bitmap;

    public MapOverlay(Resources resources, int resourceId) {

            bitmap = BitmapFactory.decodeResource(resources, resourceId);

    }

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

    ...
            canvas.drawBitmap(bmp, src, dst, null);

    }
}

答案 5 :(得分:0)

您必须在listOfOverlays添加叠加层。然后只会在地图上显示。为此,您必须使用您的overlayitem创建geopoint,然后将其添加到叠加列表中。

例如:

GeoPoint point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
OverlayItem overlayItem = new OverlayItem(point, "", "");   
listOfOverlays.addOverlay(overlayItem);
mapView.invalidate();

试试看。如果您遇到任何问题,请添加为评论。

答案 6 :(得分:0)

我只是花了很长时间把头发拉出来和你一样的问题。原来从我的MapView声明中删除 android:layout_weight =“1”解决了这个问题。不要问我为什么!