在Android应用程序上未显示的Osm磁贴

时间:2013-01-10 12:08:01

标签: android maps osmdroid

我正在尝试创建一个可以显示离线地图的应用,其中包含一些可点击的点。 我的问题是,当我测试我的应用程序时,地图图块不会显示。另外,一旦我移动地图,可点击的点(标记)就会消失。

对此问题的任何帮助或想法都会非常有帮助。

这是我的代码:

MainActivity.java: 的

package com.cvasil05.offlinemapwithoverlay;

import java.util.ArrayList;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {


// Default map zoom level:
private int MAP_DEFAULT_ZOOM = 17;

// Default map Latitude:
private double MAP_DEFAULT_LATITUDE = 35.14476619358656E6;

// Default map Longitude:
private double MAP_DEFAULT_LONGITUDE = 33.409520387649536E6;

private MapView             mymapView           = null;
private MapController       myMapController     = null;
private MyLocationOverlay   location            = null;
LocationManager             mylocmanager        = null;
LocationListener            myloclistener       = null;

ArrayList<OverlayItem>      OverlayItemArray    = null;
MyOverlayItem               overlay             = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Map with its properties
    mymapView = (MapView)findViewById(R.id.mapview);
    mymapView.setBuiltInZoomControls(false);
    mymapView.setMultiTouchControls(false);
    mymapView.setTileSource(TileSourceFactory.MAPQUESTOSM);

    // Initialize Map Controller
    myMapController = mymapView.getController();

    mylocmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    myloclistener = new LocationListener() {

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

        }

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

        }

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

        }

        public void onLocationChanged(Location location) {
            myMapController.animateTo(new GeoPoint(location.getLatitude(), location.getLongitude()));
            myMapController.setZoom(17);
            mymapView.invalidate();
        }

        public void run() {
            // TODO Auto-generated method stub
            location.runOnFirstFix(new Runnable() { 
                public void run() {
                    mymapView.getController().animateTo(location.getMyLocation());
                }
            }); 
        }
    };
    mylocmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,myloclistener);



    OverlayItemArray = new ArrayList<OverlayItem>();
    // Points to be added
    GeoPoint point1 = new GeoPoint(35.14392398396914E6, 33.40521812438965E6);
    OverlayItem overlayitem1 = (OverlayItem) new OverlayItem("Εστίες", "Οι εστίες του Πανεπιστημίου Κύπρου.", point1);
    OverlayItemArray.add(overlayitem1);

    GeoPoint point2 = new GeoPoint(35.14447668501356E6, 33.41047525405884E6);
    OverlayItem overlayitem2 = new OverlayItem("ΘΕΕ02", "Κτίριο Χημείας & Φυσικής.", point2);
    OverlayItemArray.add(overlayitem2);

    GeoPoint point3 = new GeoPoint(35.14461705293515E6, 33.41110825538635E6);
    OverlayItem overlayitem3 = new OverlayItem("ΘΕΕ01", "Κτίριο Πληροφορικής & Μαθηματικών.", point3);
    OverlayItemArray.add(overlayitem3);

    GeoPoint point4 = new GeoPoint(35.14512588462148E6, 33.41071128845215E6);
    OverlayItem overlayitem4 = new OverlayItem("ΧΩΔ01", "Χώροι Διδασκαλίας", point4);
    OverlayItemArray.add(overlayitem4);

    GeoPoint point5 = new GeoPoint(35.1459856274836E6, 33.41367244720459E6);
    OverlayItem overlayitem5 = new OverlayItem("Κέντρο αθλητισμού", "Αποτελείται από το Γυμναστήριο, το Γήπεδο Τέννις και το Γύπεδο Φούτσαλ.", point5);
    OverlayItemArray.add(overlayitem5);

    // Add Points to Overlay
    overlay = new MyOverlayItem(this, OverlayItemArray);
    mymapView.getOverlays().add(overlay);


    /*
    // Get Location
    location = new MyLocationOverlay(getApplicationContext(), mymapView);
    // View Location
    mymapView.getOverlays().add(location);
    location.enableMyLocation();
    location.disableFollowLocation();
    */


    // Zoom and Center Map
    myMapController.setZoom(MAP_DEFAULT_ZOOM);
    myMapController.setCenter(new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));

}      


// Create Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

// Actions on Menu Options
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.esties){
        myMapController.setZoom(MAP_DEFAULT_ZOOM);
        myMapController.setCenter(new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
    }
    return super.onOptionsItemSelected(item);
}



}

MyOverlayItem.java:

package com.cvasil05.offlinemapwithoverlay;

import java.util.List;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.app.AlertDialog;
import android.content.Context;

public class MyOverlayItem extends ItemizedIconOverlay<OverlayItem> {

private Context mContext;

public MyOverlayItem(final Context context, final List<OverlayItem> aList) {
    super(context, aList, new OnItemGestureListener<OverlayItem>() {
        public boolean onItemSingleTapUp(final int index,
                final OverlayItem item) {
            return false;
        }

        public boolean onItemLongPress(final int index,
                final OverlayItem item) {
            return false;
        }
    });
    // TODO Auto-generated constructor stub
    mContext = context;
}

@Override
protected boolean onSingleTapUpHelper(final int index, final OverlayItem item, final MapView mapView) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}

}

我的xml文件包含:

<org.osmdroid.views.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:enabled="true"      
        android:clickable="true"
    />

我把它添加到我的清单文件中:

<supports-screens 
android:anyDensity="true"
android:resizeable="false"
android:largeScreens="true"
android:normalScreens="true" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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" />

1 个答案:

答案 0 :(得分:1)

我是osmdroid的新手:)但我遇到了同样的问题..在我的情况下,我的模拟器设备配置了没有SDCard