2012-04-13 86 views
2

OnLocationChanged()不會調用,雖然requestLocationUpdates被添加,所以,地圖不顯示它的當前位置。
地圖始終顯示其默認模擬位置。請建議解決此問題。我已經在清單文件中添加了所有必要的權限。
注意:我不想使用getLastKnownLocation的最後位置,而是我想要當前位置。我等待設備獲得修復,有時GPS需要比平時更多的時間,但仍然沒有改進。我也用這臺設備走了近10公里。 :-)。
這是我使用的代碼。OnLocationChanged不叫

public class TestingmapActivity extends MapActivity implements LocationListener { 

private MapController myMapController; 
private MapView mapView; 
private LocationManager myLocationManager; 
private double m_dLati = 12.913469, m_dLongi = 77.609761; 
String provider; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    mapView = (MapView) findViewById(R.id.mapview); 
    myMapController = mapView.getController(); 
    provider = LocationManager.GPS_PROVIDER; 
    mapView.setBuiltInZoomControls(true); 
    mapView.setStreetView(true); 
    myMapController.setZoom(14); // Zoon 1 is world view 
    myLocationManager.requestLocationUpdates(provider, 0, 0,this); 
    setMockLocation(m_dLati, m_dLongi, 500); 
} 

@Override 
protected void onResume() { 
    startListening(); 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    stopListening(); 
    super.onResume(); 
} 

@Override 
protected void onStop() { 
    stopListening(); 
    super.onStop(); 
} 

private void startListening() { 
    if (myLocationManager != null) { 
     myLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 
} 

private void stopListening() { 
    if (myLocationManager != null) 
     myLocationManager.removeUpdates(this); 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

private void setMockLocation(double latitude, double longitude, 
     float accuracy) { 
    Log.v("", "provider name= " + provider); 
    myLocationManager.addTestProvider(provider, "requiresNetwork" == "", 
      "requiresSatellite" == "", "requiresCell" == "", 
      "hasMonetaryCost" == "", "supportsAltitude" == "", 
      "supportsSpeed" == "", "supportsBearing" == "", 
      android.location.Criteria.POWER_LOW, 
      android.location.Criteria.ACCURACY_FINE); 
    Location newLocation = new Location(provider); 
    newLocation.setLatitude(latitude); 
    newLocation.setLongitude(longitude); 
    newLocation.setAccuracy(accuracy); 
    Toast.makeText(
      getApplicationContext(), 
      "Mock: New Latitude = " + newLocation.getLatitude() 
        + " New Longitude = " + newLocation.getLongitude(), 
      Toast.LENGTH_LONG).show(); 
    myLocationManager.setTestProviderEnabled(provider, true); 
    myLocationManager.setTestProviderStatus(provider, 
      LocationProvider.AVAILABLE, null, System.currentTimeMillis()); 
    myLocationManager.setTestProviderLocation(provider, newLocation); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.v("LocationListener", "onLocationChanged " + location.getLatitude() 
      + " , " + location.getLongitude()); 
    int lat = (int) (location.getLatitude() * 1E6); 
    int lng = (int) (location.getLongitude() * 1E6); 
    GeoPoint point = new GeoPoint(lat, lng); 
    myMapController.animateTo(point);  
} 

@Override 
public void onProviderDisabled(String provider) {} 

@Override 
public void onProviderEnabled(String provider) {} 

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

清單權限:

<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.LOCATION" /> 

感謝, Biplab

+0

接受最後一個問題的答案該怎麼辦? – user370305 2012-04-13 05:14:51

+0

對不起,我不知道。我爲最後一個問題標記了接受答案滴答聲按鈕。感謝你的回答。 – 2012-04-13 05:21:50

+0

你在設備上檢查它?你在外面測試嗎? – 2012-04-13 05:25:17

回答

-1

嘗試在室外使用從room..because GPS中的居所indoor..depending不工作..它在室外完美運作..

+0

我已經覆蓋了將近10公里的區域來測試應用程序。它沒有工作。 – 2012-04-13 06:25:16

+0

我懷疑截至2012年4月,但現在絕對不是問題Hulk – ericn 2013-06-06 12:09:27

+0

可能..但在ApriL'12這是一個問題.. – Hulk 2013-06-12 06:39:05

0

@Biplab Kundu - 你是否在你的setMockLocation方法的覆蓋提供商? 請再次檢查該方法。

+0

setMockLocation不是一個重寫功能。我已經寫過,根據作爲參數傳遞的緯度和經度在地圖上設置一些位置。 – 2012-04-13 06:27:30

+0

@SandipJadhav你的意思是'LocationServices.FusedLocationApi'? – 2015-04-07 14:36:54

+0

我正在實現onLocationChangeListener – 2015-04-08 05:27:21