0

我在 OpenStreetMaps 应用程序中遇到了 MyCurrentPosition 问题。我已经使用 OSMdroid 3.0.9 库创建了该应用程序。问题是关于当前位置。问题似乎与捏放大/缩小有关。它仅在捏屏幕时向我显示 GPS 或网络提供商的正确当前位置。当我释放它时,它会改变我当前的位置与附近的另一个位置,但不是真正的一个位置。我在下面给你我的代码,让你一睹为快:

MainActivity.java

public class MainActivity extends Activity {


private MapView map;
private MapController mapController;
private MyLocationOverlay miLocalizacion;
private LocationManager locManager; 
private LocationListener locListener;
private Button pruebas;
private Location actual;
private GeoPoint punto;
private double mLatitude, mLongitude;
private Button candado;
private OnItemGestureListener<OverlayItem> myOnItemGestureListener;

public static Resources res;


private     ArrayList<OverlayItem> overlayItemArray;
final private String MAP_DEFAULT_STARTPOSITION = "Santander";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);

    overlayItemArray = new ArrayList<OverlayItem>();

    candado = (Button)findViewById(R.id.button2);

    map = (MapView)findViewById(R.id.openmapview);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.setTileSource(TileSourceFactory.MAPNIK);

    mapController = new MapController(map);
    mapController = map.getController();
    mapController.setZoom(15);

    candado.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


        }
    });



    LocationManager locManager = (LocationManager)getSystemService(LOCATION_SERVICE);



    actual = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    locListener = new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


        }

        @Override
        public void onProviderEnabled(String arg0) {


        }

        @Override
        public void onProviderDisabled(String arg0) {


        }

        @Override
        public void onLocationChanged(Location loc) {
            ActualizarPosicion(loc);

             mLatitude = (int) (loc.getLatitude() * 1E6);
                mLongitude = (int) (loc.getLongitude() * 1E6);

        }


    };

    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);


    miLocalizacion = new MyLocationOverlay(getBaseContext(), map);

    map.getOverlays().add(miLocalizacion);


    //Mi ubicación




    miLocalizacion.runOnFirstFix(new Runnable() {
        public void run() {

            map.getController().animateTo(miLocalizacion.getMyLocation());
        }
    });


    res = getResources();


    //Minimapa
    MinimapOverlay miniMapOverlay = new MinimapOverlay(this, map.getTileRequestCompleteHandler());
    miniMapOverlay.setZoomDifference(5);
    miniMapOverlay.setHeight(200);
    miniMapOverlay.setWidth(200);
    map.getOverlays().add(miniMapOverlay);



    myOnItemGestureListener = new OnItemGestureListener<OverlayItem>() {

        @Override
        public boolean onItemLongPress(int arg0, OverlayItem arg1) {
            Toast.makeText(getBaseContext(), "Pulsacion larga", Toast.LENGTH_SHORT).show();
            return false;
        }

        @Override
        public boolean onItemSingleTapUp(int arg0, OverlayItem arg1) {
            Toast.makeText(getBaseContext(), "Pulsacion corta", Toast.LENGTH_SHORT).show();
            return false;
        }
    };




    overlayItemArray.add(new OverlayItem("Hola", "Estás aquí.", new GeoPoint(actual.getLatitude(), actual.getLongitude())));
    ItemizedOverlayWithFocus<OverlayItem> anotherItemizedIconOverlay = new ItemizedOverlayWithFocus<OverlayItem>(this, overlayItemArray, myOnItemGestureListener);
    map.getOverlays().add(anotherItemizedIconOverlay);

    anotherItemizedIconOverlay.setFocusItemsOnTap(true);
    anotherItemizedIconOverlay.setFocusedItem(0);

    //Botón de pruebas
     pruebas = (Button)findViewById(R.id.button1);
     pruebas.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(getBaseContext(), actual.getLatitude() + " , " + actual.getLongitude(), Toast.LENGTH_SHORT).show();

        }
    });



protected void PonerMarcador(Location loc) {
    overlayItemArray.add(new OverlayItem("Hola", "Estás aquí.", new GeoPoint(actual.getLatitude(), actual.getLongitude())));
    ItemizedOverlayWithFocus<OverlayItem> anotherItemizedIconOverlay = new ItemizedOverlayWithFocus<OverlayItem>(this, overlayItemArray, myOnItemGestureListener);
    map.getOverlays().add(anotherItemizedIconOverlay);

    anotherItemizedIconOverlay.setFocusItemsOnTap(true);

}






protected void ActualizarPosicion(Location loc) {
actual.setLatitude(loc.getLatitude());
actual.setLongitude(loc.getLongitude());
mapController.animateTo(actual.getLatitude(),actual.getLongitude());


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Se lanza el menú, codigo para la action bar, iconos etc..
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


    private void setOverlayLoc(Location overlayloc){
        GeoPoint overlocGeoPoint = new GeoPoint(overlayloc);
        overlayItemArray.clear();
        OverlayItem newMyLocationItem = new OverlayItem("My Location", "My Location", overlocGeoPoint);
        overlayItemArray.add(newMyLocationItem);

    }



 private class MyItemizedIconOverlay extends ItemizedIconOverlay<OverlayItem>{

        public MyItemizedIconOverlay(
                List<OverlayItem> pList,
                org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,
                ResourceProxy pResourceProxy) {
            super(pList, pOnItemGestureListener, pResourceProxy);

        }

        @Override
        public void draw(Canvas canvas, MapView mapview, boolean arg2) {

            super.draw(canvas, mapview, arg2);

            if(!overlayItemArray.isEmpty()){

                //overlayItemArray have only ONE element only, so I hard code to get(0)
                GeoPoint in = overlayItemArray.get(0).getPoint();

                Point out = new Point();
                mapview.getProjection().toPixels(in, out);

                Bitmap bm = BitmapFactory.decodeResource(getResources(), 
                        R.drawable.ic_launcher);
                canvas.drawBitmap(bm, 
                        out.x - bm.getWidth()/2,    //shift the bitmap center
                        out.y - bm.getHeight()/2,   //shift the bitmap center
                        null);
            }
        }

 }


    }


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pruebas" />

    <Button
        android:id="@+id/button2"
        android:layout_width="62dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.13" />

</LinearLayout>

<org.osmdroid.views.MapView
    android:id="@+id/openmapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</org.osmdroid.views.MapView>

4

1 回答 1

0

在清单或代码中关闭硬件加速。有关如何执行此操作的示例,请参阅示例应用程序。3.0.10 中有更好的硬件加速支持。

于 2013-04-30T13:48:57.927 回答