2013-04-24 51 views
0

我有OpenStreetMaps应用程序中的MyCurrentPosition问题。我已经使用OSMdroid 3.0.9库创建了该应用程序。 这个问题是关于当前的位置。这个问题似乎是关于捏放大/缩小的问题。它只会显示GPS或NETWORK提供商正确的当前位置,同时捏住屏幕。在我释放它的时候,它改变了我目前的位置与另一个附近,但不是真正的一个位置。我下面给你我的代码,以便在拍摄一瞥:改变我的位置捏 - OpenStreetsMaps

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> 

+0

我认为这可能是miLocalizacion.enableMyLocation(); 我把它打开了,我在屏幕中央得到了黄色的像男人一样的标记,但是没有在我目前的位置,只有当我捏出/插入时才能正常工作。当我松开手指时,它再次进入错误的位置:\ – leonis 2013-04-30 10:09:54

回答

0

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

+0

非常感谢!我已降级到3.0.8,并通过manifest.xml中的android:hardwareAccelerated =“false”行解决了这个问题。 再次感谢! – leonis 2013-05-06 09:21:17

+0

我们最近推出了3.0.10,其中包含各种修复程序。你应该试试看。报告错误回到谷歌代码页面。 – kurtzmarc 2013-05-06 14:08:21