2013-02-27 70 views
1

Google終於在Android API v2中添加了位置更改的回調!但是,我無法直觀地使其發揮作用,Google沒有太多的文檔。有沒有人得到它的工作?我還需要什麼?在Google Maps中使用新的OnMyLocationChangeListener Android API v2

public class ... extends SupportMapFragment implements GoogleMap.OnMyLocationChangeListener { 
GoogleMap map; 
LocationManager locationManager; 
String provider; 

     @Override 
     public void onActivityCreated(android.os.Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      map = getMap(); 
        if (map != null) { 
         Criteria criteria = new Criteria(); 
         criteria.setAccuracy(Criteria.ACCURACY_FINE); 
          locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
       provider = locationManager.getBestProvider(criteria, false); 
      } 
     } 

     @Override 
     public void onResume() { 
      super.onResume(); 
      while(map == null) { 
       map = getMap(); 
       map.setMyLocationEnabled(true); 
       map.setOnMyLocationChangeListener(this); 
      } 
     } 
     @Override 
     public void onMyLocationChange(Location loc) { 
      //implementation 
     } 
} 
+0

小心解釋你爲什麼低估了我? – brainmurphy1 2013-02-27 04:16:46

+0

我結束了使用這一個http://stackoverflow.com/questions/13742551/how-to-get-my-location-changed-event-with-google-maps-android-api-v2 – brainmurphy1 2013-07-02 00:33:12

回答

7

當我們獲得第一個位置更新時,這就是我如何導航到地圖的中心。

我的類標頭:

public class FragActivity extends SherlockFragmentActivity implements OnMyLocationChangeListener 

private GoogleMap mMap; 

我的MMAP-設置:

if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = customMapFragment.getMap(); 

     // Check if we were successful in obtaining the map. 
     if (mMap != null) 
      setUpMap(); 
    } 

setUpMap-方法:

private void setUpMap() { 
    mMap.setMyLocationEnabled(true); 
    mMap.setOnMyLocationChangeListener(this); 
} 

和我的onlocationchange:

@Override 
public void onMyLocationChange(Location lastKnownLocation) { 
    CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
      new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(), 
        lastKnownLocation.getLongitude())).zoom(6).build()); 
    mMap.moveCamera(myLoc); 
    mMap.setOnMyLocationChangeListener(null); 
} 

作品像一個魅力

+0

我做到了這一點,但是,第一個位置更改更新永遠不會到來。我正在從「customMapFragment」內執行此過程。這可能是問題嗎? – brainmurphy1 2013-02-27 14:13:29

+0

我使用了以「更新」開頭的解決方案。 http://stackoverflow.com/questions/13742551/how-to-get-my-location-changed-event-with-google-maps-android-api-v2 – brainmurphy1 2013-02-27 15:43:59

+0

你必須有谷歌播放服務rev> = 5安裝使用OnMyLocationChangeListener – stoefln 2013-03-07 13:05:54

相關問題