当设备在Android中移动时,如何在谷歌地图上移动标记(当前标记)

时间:2011-09-15 09:42:32

标签: android android-maps

当设备移动(用户移动)时,如何在谷歌地图上移动标记(如点).....

import android.content.Context;
import android.widget.Toast;

import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

// This class subclasses (extends) MyLocationOverlay so that we can override its dispatchTap method
// to handle tap events on the present location dot.

public class MyMyLocationOverlay extends MyLocationOverlay {

private Context context;

public MyMyLocationOverlay(Context context, MapView mapView) {
    super(context, mapView);
    this.context = context;   // Will need this for Toast argument below
}

// Override the dispatchTap() method to toggle the data display on and off when
// the present location dot is tapped. Also display a short Toast (transient message) to the
// user indicating the display status change.

@Override
protected boolean dispatchTap(){
    if(DisplayOverlay.showData){ 
        Toast.makeText(context, "Suppressing data readout", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(context,"Display data readout", Toast.LENGTH_SHORT).show();
    }
    // Toggle the GPS data display
    DisplayOverlay.showData = ! DisplayOverlay.showData;
    return true;
}

1 个答案:

答案 0 :(得分:2)

检查我的帖子回答是否有链接。

how to display map in android with marker

要在地图上移动标记,您需要在您的类中创建扩展MapActivity的静态方法,如

private static GeoPoint markerPoint;

public static void updateLocation(Location location){
   lat = location.getLatitude();
   lng = location.getLongitude();

   // now use this lat/lng value to convert into the GeoPoint object

   markerPoint = new GeoPoint(lat * 1e6, lng * 1e6);
   mapview.invalidate();
}

你的自定义覆盖类,它扩展了Overlay类。

public void draw(Canvas canvas, MapView mapView, boolean Shadow){
    Point screenPts = new Point();
    mapView.getProjection().toPixels(markerPoint, screenPts);
    //---add the marker---
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.cur_loc_1);
    canvas.drawBitmap(bmp, screenPts.x-(xCurLocOffset), screenPts.y-yCurLocOffset, null);
}

此处显示使用Bitmap类在地图上绘制图像的可绘制图像显示。