2012-01-10 216 views
1

我正在測試一個獲取sumsung galaxy spica當前位置的應用程序,它向我展示了另一個不同於我的位置,它與AVD中顯示的不一樣! GPS已激活,但我不知道問題出在哪裏。 這裏是我的代碼android獲取當前位置

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.widget.Toast; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 

public class MaptestActivity extends MapActivity implements LocationListener { 
    private MapView mapView = null; 
    private LocationManager lm = null; 
    private double lat = 0; 
    private double lng = 0; 
    private MapController mc = null; 
    private MyLocationOverlay myLocation = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mapView = (MapView) this.findViewById(R.id.mapView); 
    mapView.setBuiltInZoomControls(true); 

    lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this); 
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this); 




    myLocation = new MyLocationOverlay(getApplicationContext(), mapView); 
    myLocation.runOnFirstFix(new Runnable() { 
     public void run() { 
     mc.animateTo(myLocation.getMyLocation()); 
     mc.setZoom(17); 
     mc = mapView.getController(); 
     lat = myLocation.getMyLocation().getLatitudeE6(); 
     lng = myLocation.getMyLocation().getLongitudeE6(); 
     Toast.makeText(
       getBaseContext(), 
       "My Location : Latitude = " + lat + " Longitude = " 
        + lng, Toast.LENGTH_LONG).show(); 
     } 
    }); 
    mapView.getOverlays().add(myLocation); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    myLocation.enableMyLocation(); 
    myLocation.enableCompass(); 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
    return false; 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_S) { 
     mapView.setSatellite(!mapView.isSatellite()); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
    } 

    public void onLocationChanged(Location location) { 
    lat = location.getLatitude(); 
    lng = location.getLongitude(); 
    Toast.makeText(
     getBaseContext(), 
     "Location change to : Latitude = " + lat + " Longitude = " 
      + lng, Toast.LENGTH_SHORT).show(); 
    GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
    mc.animateTo(p); 
    mc.setCenter(p); 
    } 


    public void onProviderDisabled(String provider) { 
    } 


    public void onProviderEnabled(String provider) { 
    } 


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

} 

這裏是我的清單

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

    <uses-sdk android:minSdkVersion="3" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <uses-library android:name="com.google.android.maps" /> 
     <activity 
      android:name=".MaptestActivity" 
      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.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE" /> 
    <uses-permission android:name="android.permission.LOCATION" /> 

</manifest> 

,並在這裏的錯誤在logcat的

01-10 10:37:47.765: E/Sensors(5222): ####### akmd2 started!!! 
01-10 10:37:47.770: E/SensorManager(5222): registerListener 0:AK8973 Compass 
01-10 10:37:47.950: E/SensorManager(5222): =======>>>>>>> Sensor Thread RUNNING <<<============================ 
01-10 10:37:47.995: I/MapActivity(5222): Handling network change notification:CONNECTED 
01-10 10:37:47.995: E/MapActivity(5222): Couldn't get connection factory client 

感謝

+0

你是什麼意思,它不同於AVD?它顯然應該是不同的。 – Reno 2012-01-10 12:55:06

+0

是的,我知道,但如果它不同,那就意味着它是正確的!在我的情況下,在AVD它顯示塔爾薩!並在我的手機,它告訴我另一個地方(bievre igny)在法國和我目前的位置,我現在在突尼斯 – 2012-01-10 13:17:14

+0

你可以編輯你的問題,包括你在'onLocationChanged()'得到的經度。另請注意'lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,0,this);' – Reno 2012-01-10 13:33:32

回答

1

我有同樣的問題解決方案提

你不必MapActivity延伸,同時獲得GPS信號

使用活動隨後,盼着到MapActivity

雖然不知道原因:(