Osmdroid Overlays崩溃应用程序(Android)

时间:2011-06-25 01:23:14

标签: android osmdroid

package gpstest.example.com;



import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.content.Context;
import android.graphics.Paint;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;




public class gpstestActivity extends Activity

{
    public LocationManager mlocManager;
    public LocationListener mlocListener;
    public boolean isFirstRun=true;
    protected final Paint mPaint = new Paint();
    private MapView myMap;
    private MyLocationOverlay myLocOverlay;




/** Called when the activity is first created. */






public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);


setContentView(R.layout.main);

gpsEnable();
setMap();
initMyLocation();


}




public void setMap(){
    MapView map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);

    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.getController().setZoom(16);
    map.getController().setCenter(new GeoPoint(30266000, -97739000));

}

private void initMyLocation() {
    myLocOverlay = new MyLocationOverlay(this, myMap);
    myLocOverlay.enableMyLocation();
    myMap.getOverlays().add(myLocOverlay);

}



public void gpsEnable (){
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    mlocListener = new MyLocationListener();

    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10, 50, mlocListener);
}

public void gpsDisable(){
    mlocManager.removeUpdates(mlocListener);}


/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();
double longi=loc.getLongitude();
double lati=loc.getLatitude();
MapView map = (MapView) findViewById(R.id.map);
map.getController().setCenter(new GeoPoint(lati, longi));

String Text="My current location is: " +

"Latitud = " + loc.getLatitude() +

"Longitud = " + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();
//initMyLocation();

}

@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(),

"Gps Disabled",

Toast.LENGTH_SHORT ).show();
mlocManager.removeUpdates(mlocListener);
}


@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(),

"Gps Enabled",

Toast.LENGTH_SHORT).show();

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{
    Toast.makeText( getApplicationContext(),

            "Status changed",

            Toast.LENGTH_SHORT).show();

}
}/* End of Class MyLocationListener */





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.beenden:  mlocManager.removeUpdates(mlocListener);}


        return true;
    }




protected void onPause(){
    Toast.makeText( getApplicationContext(),

        "On Pause",

            Toast.LENGTH_SHORT).show();
    gpsDisable();
    isFirstRun=false;

    super.onPause();

}

protected void onDestroy(){
    Toast.makeText( getApplicationContext(),

            "Destroyed",

            Toast.LENGTH_SHORT).show();
    gpsDisable();
    super.onDestroy();

}

protected void onResume(){
    if (isFirstRun==false){gpsEnable();}
    Toast.makeText( getApplicationContext(),

    "Resumed",

    Toast.LENGTH_SHORT).show();

    super.onResume();
}







}/* Ende Activity */

每当我调用initMyLocation时,应用程序崩溃(模拟器和设备上的bot)。当我尝试实现MiniMap时也是如此。我与Osmdroid存储库中的SampleExtensive示例完全相同。我做错了什么?

我的AndroidManifest.xml提供了我认为的所有必需权限

<?xml version="1.0" encoding="utf-8"?>

    

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".gpstestActivity"
              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>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>  
<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" />

2 个答案:

答案 0 :(得分:1)

要使用位置,您必须在AndroidManifest.xml中包含以下内容:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

答案 1 :(得分:1)

在你的setMap()中,你永远不会设置

myMap = map;

因此在initMyLocation中,您为pMapView参数传递null。