【问题标题】:marker in the osm Map with osmdroidosm 映射中的标记与 osmdroid
【发布时间】:2012-07-16 13:47:07
【问题描述】:

我试图在 OSM 中显示一个标记,但不能。我可以显示 OSM 地图和手机的位置,但不能显示其他内容。

这是我的代码:

public class Carte2Activity extends Activity implements LocationListener {
    MapView mapView;
    MapController myMapController;
    MyLocationOverlay location = null;
    double lat;
    double lng;
    LocationManager lm;
    LocationListener ll;

    ArrayList<OverlayItem> anotherOverlayItemArray;
    String[] sources;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        mapView = (MapView)findViewById(R.id.openmapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);

        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        ll = new Myll();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,ll);

        myMapController = mapView.getController();
        myMapController.setZoom(10);
        myMapController.setCenter(new GeoPoint((int)(48.785152*1E6), (int)(2.285156*1E6)));


        location = new MyLocationOverlay(getApplicationContext(), mapView);

       //ajouter la loc
        mapView.getOverlays().add(location);
        //afficher
        location.enableMyLocation();

    }      

    private class Myll implements LocationListener{

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        lat = location.getLatitude();
        lng = location.getLongitude();
        run();
    }

    public void run() {
        // TODO Auto-generated method stub
        location.runOnFirstFix(new Runnable() { public void run() {
            mapView.getController().animateTo(location.getMyLocation());
            }}); 
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub      
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub      
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub      
    }
}

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub      
    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub      
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub      
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

androidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="comparateur.carte"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Carte2Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我不知道该怎么做。有人可以帮帮我吗?

【问题讨论】:

    标签: java android map marker osmdroid


    【解决方案1】:

    看看这几行代码:

            OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation);
            Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.person);
            myLocationOverlayItem.setMarker(myCurrentLocationMarker);
    
            final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
            items.add(myLocationOverlayItem);
    
            currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                    new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                        public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                            return true;
                        }
                        public boolean onItemLongPress(final int index, final OverlayItem item) {
                            return true;
                        }
                    }, resourceProxy);
            this.mapView.getOverlays().add(this.currentLocationOverlay);
    

    希望对你有帮助。

    【讨论】:

    • 我在这一行得到空指针异常:currentLocationOverlay = new ItemizedIconOverlay(items, new ItemizedIconOverlay.OnItemGestureListener() { public boolean onItemSingleTapUp(final int index, final OverlayItem item) { return true; } public boolean onItemLongPress(final int index, final OverlayItem item) { return true; } }, resourceProxy);
    【解决方案2】:

    添加标记他们都吃了:)

    工作代码:

    public class mapcode extends Activity {
        globalvar appState;
        int stats=0;
        private MapView mapView;
        private IMapController mapController;
        private SimpleLocationOverlay mMyLocationOverlay;
        private ScaleBarOverlay mScaleBarOverlay;  
        ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
        DefaultResourceProxyImpl resourceProxy;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            setContentView(R.layout.map);
    
            appState = ((globalvar) getApplicationContext());
    
    
    
    
    
            mapView = (MapView) this.findViewById(R.id.mapview);  
            mapView.setTileSource(TileSourceFactory.MAPNIK);
          //  mapView.setBuiltInZoomControls(true); //кнопка ZOOM +-
            mapView.setMultiTouchControls(true);
    
            mapController = this.mapView.getController();
            mapController.setZoom(2);
    
            this.mMyLocationOverlay = new SimpleLocationOverlay(this);                          
            this.mapView.getOverlays().add(mMyLocationOverlay);
    
            this.mScaleBarOverlay = new ScaleBarOverlay(this);                          
            this.mapView.getOverlays().add(mScaleBarOverlay);
    
    
            /////////////////
            resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
            GeoPoint  currentLocation = new GeoPoint(55.860863,37.115046); 
            GeoPoint  currentLocation2 = new GeoPoint(55.8653,37.11556); 
            OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation);
            Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
            myLocationOverlayItem.setMarker(myCurrentLocationMarker);
    
            final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
            items.add(myLocationOverlayItem);
    
    
    
          myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation2);
         myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
            myLocationOverlayItem.setMarker(myCurrentLocationMarker);
    
    
            items.add(myLocationOverlayItem);
    
    
    
            currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                    new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                        public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                            return true;
                        }
                        public boolean onItemLongPress(final int index, final OverlayItem item) {
                            return true;
                        }
                    }, resourceProxy);
            this.mapView.getOverlays().add(this.currentLocationOverlay);
    
    }
    

    【讨论】:

      【解决方案3】:
       public void showOnMap(double lat , double lon){
          map = (MapView) root.findViewById(R.id.map);
          map.setTileSource(TileSourceFactory.MAPNIK);
          map.setBuiltInZoomControls(true);
          map.setMultiTouchControls(true);
          mapController = map.getController();
          myLocationoverlay = new MyLocationNewOverlay(map);
          myLocationoverlay.setEnabled(true);
          map.getOverlays().add(myLocationoverlay);
          GeoPoint startPoint = new GeoPoint(lat, lon);
          mapController.animateTo(startPoint);
          mapController.setZoom(16.0);
          Marker startMarker = new Marker(map);
          startMarker.setIcon(getResources().getDrawable(R.drawable.location));
          startMarker.setPosition(startPoint);
          map.getOverlays().add(startMarker);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-27
        • 1970-01-01
        • 2021-11-07
        • 1970-01-01
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        相关资源
        最近更新 更多