2011-06-21 50 views
2

我正在開發基於谷歌地圖的應用程序,用戶必須在地圖中顯示當前位置。我有谷歌它,並找到解決方案,但是當我運行的代碼它不顯示當前的位置,而是它顯示谷歌地圖單獨。我的代碼是低於如何在Android中通過GPS獲取當前位置

public class GPSLocation extends MapActivity { 
private MapView mapView; 
private MapController mapController; 
private LocationManager locationManager; 
private GPSLocationListener locationListener; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // mapView = (MapView) findViewById(R.id.mapView); 

    // enable Street view by default 
    // mapView.setStreetView(true); 

    // enable to show Satellite view 
    // mapView.setSatellite(true); 

    // enable to show Traffic on map 
    // mapView.setTraffic(true); 

    // mapView.setBuiltInZoomControls(true); 

    // mapController = mapView.getController(); 
    // mapController.setZoom(16); 




    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    locationListener = new GPSLocationListener(); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 


} 
private class GPSLocationListener implements LocationListener 
{ 
    public GPSLocationListener() { 
     // TODO Auto-generated constructor stub 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
    if (location != null) { 
     System.out.println("onLocationChanged"); 
      GeoPoint point = new GeoPoint(
        (int) (location.getLatitude() * 1E6), 
        (int) (location.getLongitude() * 1E6)); 


     Toast.makeText(getBaseContext(), 
      "Latitude: " + location.getLatitude() + 
      " Longitude: " + location.getLongitude(), 
      Toast.LENGTH_SHORT).show(); 

     mapController.animateTo(point); 
     mapController.setZoom(16); 
     mapView.invalidate(); 
    } 
    else 
    { 
     System.out.println("OnLocationChanged value is null"); 
    } 
    } 



@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 


@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 


@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

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





} 

我不知道我犯了什麼錯誤。它甚至沒有進入GPSLocationListener類。請幫助我.. 在此先感謝。

+0

謝謝我從你的代碼中得到了很多幫助。 :) –

回答

1

檢查是您的GPS活躍。更改最短距離和最短時間。例如:

requestLocationUpdates(LocationManager.GPS_PROVIDER,10,30,locationListener);
+0

我已經嘗試過,但它再次顯示只有地圖不是當前位置。 – deepa

+0

我正在獲取當前位置.. –

0

你正在一個設備或模擬器上測試嗎?因爲在模擬器上你必須手動輸入座標。

此外,確保follwing的一個是在你的manifest文件:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" /> 
+0

我只在模擬器中檢查。如果我給出了座標,它必須運行GPSLocationListener中的代碼,但它甚至不會進入該類,那麼它的可能性如何?是的,我已經在清單文件中給出了許可 – deepa

+0

在eclipse中,點擊頂部工具欄中的'窗口',然後'顯示視圖',並查找'模擬器控制',如果它不在該列表中,點擊'其他'並從那裏選擇它。然後,在出現的視圖中滾動到它所說的「位置控件」的底部並更改其中的座標,然後按'發送'。 – JDx

+0

我已將模擬器控制lang中的座標更改爲9 58'並且長度爲78 10'作爲當前位置,但它只是再次顯示地圖而不是特定位置。並沒有進入GPSLocationListener類.. – deepa

0

請添加下面這行代碼。它爲我工作。

LocationManager.requestLocationUpdates(LocationManager.NETWORk_PROVIDER,0,0,locationListener); 

如果它仍然不起作用可以請給出日誌詳細信息?

+0

@dukeling ..感謝您的編輯.. –

0

最近Google Maps V2可讓您執行OnMyLocationChangeListener,因此您無需實施(並設置)自定義的LocationSource。