在它被触摸的地图上的绘图图标

时间:2013-01-04 13:56:19

标签: android android-mapview ontouchevent ontouchlistener geopoints

我试图在地图上绘制一些图标,只要它被触摸。我想我快到了。 这是代码。

package com.example.googledemo4;

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.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class GoogleDemo4 extends MapActivity{

MapController mc;
MapView mapView;
GeoPoint g;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_demo4);

    mapView=(MapView) findViewById(R.id.MapView);
    mapView.setBuiltInZoomControls(true);


    mc=mapView.getController();

    List<Overlay> overlays = mapView.getOverlays();

    GeoPoint geoPoint = new GeoPoint( 18533000,73850000);


    MapOverlay4 mapOverlay = new MapOverlay4(geoPoint,mapView,this);

    overlays.add(mapOverlay);

    mc.animateTo(geoPoint);
    mc.setZoom(17);
    geoPoint=g;
    mapView.setOnTouchListener(touch);

    mapView.postInvalidate();
    }

private OnTouchListener touch = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        mapView=(MapView) findViewById(R.id.MapView);

        if (event.getAction() == 1) {                
           GeoPoint  geoPoint = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
           g=geoPoint;
                Toast.makeText(getBaseContext(), 
                    geoPoint.getLatitudeE6() / 1E6 + "," + 
                    geoPoint.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();

        }

        return true;
    }
};

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}


}

我创建了一个自定义叠加层:

package com.example.googledemo4;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.webkit.WebView.FindListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapOverlay4 extends Overlay{

GeoPoint p ;
MapView mapView;
Context context;

public MapOverlay4(GeoPoint geoPoint, MapView mapView, Context context){

    this.mapView=mapView;
    this.p=geoPoint;
    this.context=context;
}
public void draw(Canvas canvas, MapView mapView, boolean flag) {
    // TODO Auto-generated method stub
    super.draw(canvas, mapView, flag);
      //---translate the GeoPoint to screen pixels---
    mapView=this.mapView;
    Point screenPts = new Point();
    mapView.getProjection().toPixels(p, screenPts);

    //---add the marker---
    Bitmap bmp = BitmapFactory.decodeResource(
        this.context.getResources(), R.drawable.ic_launcher);            
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         


}


}

此代码首次绘制图标,因为我将geopoint初始化为某个lat和long。但是在触摸地图之后,没有任何反应。我想当地图触及地理点时会发生变化,并且更改的地点会被提供给叠加层。

此致

2 个答案:

答案 0 :(得分:0)

尝试使用itemized overlay绘制图片 并在某一点绘制覆盖OntouchEvent方法并将其传递给纬度和长度,您将在触摸位置获得图像 如果你需要一些例子,你可以看到here

答案 1 :(得分:0)

在onTouchListener中没有类似于overlays.add(mapOverlay)的调用与新的Geopoint。我想这样做必须帮助你。