Question

I have a problem with MyCurrentPosition in a OpenStreetMaps application. I've created the app with OSMdroid 3.0.9 library. The issue is about current position. The problem appears to be something about pinch to zoom in/out. It only shows me the correct current position of GPS or NETWORK provider while pinching the screen. At the time I release it, it changes my current position with another nearby, but not for real one position. I give you my code below, to taking a glimpse:

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>

Was it helpful?

Solution

Turn off hardware acceleration in your manifest or in code. See the samples application for examples on how to do this. There is better hardware acceleration support coming in 3.0.10.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top