1

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

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


}


}

这段代码第一次绘制图标是因为我将地理点初始化为一些纬度和经度。但是在触摸地图之后,什么也没有发生。我认为每当地图接触到地理点时,地理点就会改变,而改变的地理点会被提供给叠加层。

问候

4

2 回答 2

0

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

于 2013-01-04T14:10:32.760 回答
0

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

于 2013-01-04T14:06:45.490 回答