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 时,应用程序就会崩溃(模拟器和设备上的机器人)。当我尝试实现 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" />