2011-06-25 149 views
0
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示例完全相同。我究竟做錯了什麼?Osmdroid覆蓋崩潰應用程序(Android)

我的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" /> 

+0

logcat的錯誤和堆棧跟蹤是什麼? –

回答

1

在您的setMap()中,您從未設置過

myMap = map;

因此,在initMyLocation中,您爲pMapView參數傳遞null。

+0

非常感謝,修復它。 – FWeigl

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>