2013-04-11 46 views
1

我在尋找一段時間,爲什麼我的Google地圖v2沒有收到任何更新。谷歌地圖api v2 Android沒有更新的位置監聽器

我一直在閱讀/尋找幾種解決方案,但無法找到解決方案。 因此我不認爲這個話題是重複的。因爲我沒有找到的解決方案。

的代碼,我現在使用從以下主題:https://stackoverflow.com/a/14305851/1364241

public class RouteGroupMapActivity extends SherlockFragmentActivity { 

private ActionBar actionbar; 
private GoogleMap mMap; 
private FollowMeLocationSource followMeLocationSource; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_route_group_map); 
    //Activate ActionBar Sherlock 
    actionbar = getSupportActionBar(); 
    actionbar.setHomeButtonEnabled(true); 
    actionbar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE); 

    // creates our custom LocationSource and initializes some of its members 
    followMeLocationSource = new FollowMeLocationSource(); 

    //setUpMapIfNeeded(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getSupportMenuInflater(); 
    inflater.inflate(R.menu.activity_home, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); //Back button 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onPause() { 
    /* Disable the my-location layer (this causes our LocationSource to be automatically deactivated.) */ 
    mMap.setMyLocationEnabled(false); 

    super.onPause(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    /* We query for the best Location Provider everytime this fragment is displayed 
    * just in case a better provider might have become available since we last displayed it */ 
    followMeLocationSource.getBestAvailableProvider(); 

    setUpMapIfNeeded(); 

    /* Enable the my-location layer (this causes our LocationSource to be automatically activated.) 
    * While enabled, the my-location layer continuously draws an indication of a user's 
    * current location and bearing, and displays UI controls that allow a user to interact 
    * with their location (for example, to enable or disable camera tracking of their location and bearing).*/ 
    mMap.setMyLocationEnabled(true); 
} 

/** 
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
* installed) and the map has not already been instantiated.. This will ensure that we only ever 
* call {@link #setUpMap()} once when {@link #mMap} is not null. 
* <p> 
* If it isn't installed {@link SupportMapFragment} (and 
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
* install/update the Google Play services APK on their device. 
* <p> 
* A user can return to this FragmentActivity after following the prompt and correctly 
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not have been 
* completely destroyed during this process (it is likely that it would only be stopped or 
* paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in 
* {@link #onResume()} to guarantee that it will be called. 
*/ 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 

      // Replace the (default) location source of the my-location layer with our custom LocationSource 
      mMap.setLocationSource(followMeLocationSource); 

      // Set default zoom 
      mMap.moveCamera(CameraUpdateFactory.zoomTo(15f)); 
     } 
    } 
} 

private class FollowMeLocationSource implements LocationSource, LocationListener { 

    private OnLocationChangedListener mListener; 
    private LocationManager locationManager; 
    private final Criteria criteria = new Criteria(); 
    private String bestAvailableProvider; 
    /* Updates are restricted to one every 10 seconds, and only when 
    * movement of more than 10 meters has been detected.*/ 
    private final int minTime = 10000;  // minimum time interval between location updates, in milliseconds 
    private final int minDistance = 10; // minimum distance between location updates, in meters 

    private FollowMeLocationSource() { 
     // Get reference to Location Manager 
     locationManager = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE); 

     // Specify Location Provider criteria 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAltitudeRequired(true); 
     criteria.setBearingRequired(true); 
     criteria.setSpeedRequired(true); 
     criteria.setCostAllowed(true); 
    } 

    private void getBestAvailableProvider() { 
     /* The preffered way of specifying the location provider (e.g. GPS, NETWORK) to use 
     * is to ask the Location Manager for the one that best satisfies our criteria. 
     * By passing the 'true' boolean we ask for the best available (enabled) provider. */ 
     bestAvailableProvider = locationManager.getBestProvider(criteria, true); 
    } 

    /* Activates this provider. This provider will notify the supplied listener 
    * periodically, until you call deactivate(). 
    * This method is automatically invoked by enabling my-location layer. */ 
    @Override 
    public void activate(OnLocationChangedListener listener) { 
     // We need to keep a reference to my-location layer's listener so we can push forward 
     // location updates to it when we receive them from Location Manager. 
     mListener = listener; 

     // Request location updates from Location Manager 
     if (bestAvailableProvider != null) { 
      locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this); 
     } else { 
      // (Display a message/dialog) No Location Providers currently available. 
     } 
    } 

    /* Deactivates this provider. 
    * This method is automatically invoked by disabling my-location layer. */ 
    @Override 
    public void deactivate() { 
     // Remove location updates from Location Manager 
     locationManager.removeUpdates(this); 

     mListener = null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     /* Push location updates to the registered listener.. 
     * (this ensures that my-location layer will set the blue dot at the new/received location) */ 
     if (mListener != null) { 
      mListener.onLocationChanged(location); 
     } 

     /* ..and Animate camera to center on that location ! 
     * (the reason for we created this custom Location Source !) */ 
     mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()))); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 
    } 

} 

我的權限:

<uses-permission android:name="com.wandelen.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<!-- 
    The following two permissions are not required to use 
    Google Maps Android API v2, but are recommended. 
--> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

除此之外我沒有還檢查了Android谷歌地圖應用程序,它接收我的位置沒有任何問題。

而且我也把斷點在我FollowMeLocationSource的所有功能,但它並沒有對所有功能非打破..

我在想什麼?或者我可以嘗試接收我現在的位置onload?

+0

所以你只是想知道你的當前位置是什麼一次,當應用程序加載? – tyczj

+0

當我從帖子中讀到時,我也提到過,這段代碼應該做的不是它嗎?但那就是我想要的。我沒有時間去外面去測試改變是否奏效..但是我想,如果我理解得很好,它也會加載初始狀態? – spons

回答

4

如果你只需要一次性的位置,我會使用getLastKnowLocationLocationManager那樣,你不會總是沒有理由的GPS運行。

編輯:

那麼首先我不明白爲什麼你需要有一個整體的大類來獲得位置。地圖本身確實有一個位置監聽器

GoogleMap map = getMap(); 
map.setOnMyLocationChangeListener(this); 

然後在地圖片段實施OnMyLocationChangeListener當您的位置改變加在第一次運行您的初始位置像你想你會得到回調。

這樣的位置類基本上是矯枉過正和無用的,除非你需要位置管理器的特殊東西。

其次,當你創建FollowMeLocationSource類我沒有看到任何地方你請求位置更新,這將是它爲什麼不解僱

+0

但等待我的問題也是如果和爲什麼它不是由支持的代碼提供的。我會嘗試手動添加它。但我的文章中的代碼應該安排getLastKnowLocation。還開始沒有?但事件不會發生?我想解決這個問題..不要繞過它..如果沒有其他答案,我會在後面記住你的解決方案。 – spons

+0

更新了我的答案 – tyczj