0

我正在为 androd 开发一个小应用程序,以使用位图显示 gps 位置。我在 google mmaps 中使用了一些示例代码,但我必须使用 maps forge 来制作。不幸的是,我还没有找到任何示例代码或任何关于如何编写这个绘图操作的东西 Colud 有人帮我吗?

我的代码是:

    package hu.uniobuda.nik.k44hvx;



 import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.mapsforge.android.maps.GeoPoint;
import org.mapsforge.android.maps.ItemizedOverlay;
import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapController;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.MapViewMode;
import org.mapsforge.android.maps.Overlay;
import org.mapsforge.android.maps.OverlayItem;
import org.mapsforge.android.maps.Projection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import android.widget.Toast;

public class Night_WatchActivity extends MapActivity  {


    MapView mapview;
    public MapController mc;

    private LocationManager locman;
    private LocationListener loclis;

    List<Overlay> mapOverlays;
    static Drawable pointDefaultMarker;

    MapOverlay pointsOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mapview = new MapView(this);
        mapview.setClickable(true);
        mapview.setBuiltInZoomControls(true);
        mapview.setMapViewMode(MapViewMode.MAPNIK_TILE_DOWNLOAD);
       // mapview.setMapViewMode(MapViewMode.CANVAS_RENDERER);
        //mapview.setMapFile("/sdcard/budapest.map");

        mc = mapview.getController();
        mc.setCenter(new GeoPoint(47.499619,19.046406));

        mc.setZoom(17);

        setContentView(mapview);


        locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       // loclis = new GPSLocationListener();
        if(pointDefaultMarker==null){pointDefaultMarker = getResources().getDrawable(R.drawable.kepont);}
        mapOverlays = mapview.getOverlays();
        pointsOverlay = new MapOverlay(pointDefaultMarker);
        mapOverlays.add(pointsOverlay);



        locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loclis);
    }


    @Override
    protected void onResume() {
        locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000, 10, loclis);
        super.onResume();
    }

    @Override
    protected void onPause() {
        locman.removeUpdates(loclis);
        super.onPause();
    }

private class GPSLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            if(location!=null){
                GeoPoint gp = new GeoPoint(
                                    (int)(location.getLatitude()*1E6),
                                    (int)(location.getLongitude()*1E6));


                OverlayItem item = new OverlayItem(gp,null,null);

                pointsOverlay.justOneOverlay(item);
                mc.setZoom(16);
                mc.setCenter(gp);
                mapview.invalidate();



                String address = PointToLocation(gp);
                Toast.makeText(getBaseContext(), 
                          address,
                          Toast.LENGTH_SHORT).show();
            }

        }

        public void onProviderDisabled(String provider) {


        }

        public void onProviderEnabled(String provider) {


        }

        public void onStatusChanged(String provider, int status, Bundle extras) {


        }

        public String PointToLocation(GeoPoint hely){
            String s ="";


            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
            try{

                List<Address> adresses = gcd.getFromLocation(
                                            hely.getLatitudeE6() / 1E6, 
                                            hely.getLongitudeE6()/1E6 , 1);
                if(adresses.size()>0){
                    for(int i = 0; i < adresses.get(0).getMaxAddressLineIndex(); i++){
                        s += adresses.get(0).getAddressLine(i) + " ";

                    }
                }


            }catch(IOException exception){ exception.printStackTrace();}


            return s;
        }

    }

 class MapOverlay extends ItemizedOverlay<OverlayItem>{

    private GeoPoint toDraw;


    public GeoPoint getToDraw() {
        return toDraw;
    }


    public void setToDraw(GeoPoint toDraw) {
        this.toDraw = toDraw;
    }


    Paint p = new Paint();
    private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
    private boolean drawLines = false;



    public MapOverlay(Drawable defaultMarker) {
        super(boundCenter(defaultMarker));

        populate();
    }

    public void addOverlay(OverlayItem overlay){
        overlays.add(overlay);

        populate();
    }
    public void justOneOverlay(OverlayItem newOverlay){
        overlays.clear();
        addOverlay(newOverlay);             
    }
    @Override
    protected OverlayItem createItem(int i) {
        return overlays.get(i);
    }
    @Override
    public int size() {
        return overlays.size();
    }

    public boolean isDrawLines() {
        return drawLines;
    }
    public void setDrawLines(boolean drawLines) {
        this.drawLines = drawLines;     
    }   

    @Override
    protected void drawOverlayBitmap(Canvas canvas, Point drawPosition,
            Projection projection, byte drawZoomLevel) {

        Point ScreenPoint = new Point();
        mapview.getProjection().toPixels(toDraw, ScreenPoint);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.kepont);
        canvas.drawBitmap(bmp,ScreenPoint.x,ScreenPoint.y-24, null);
    }



 } 


}
4

0 回答 0