2014-03-29 136 views
0

標題幾乎涵蓋了它。我想使用手機的GPS來跟蹤用戶的位置,並在地圖上顯示標記。我希望這個標記幾乎不斷地更新它的位置,以便如果用戶移動標記也會移位。如何跟蹤和顯示用戶在地圖上的位置

我可以很好地顯示地圖,但我無法弄清楚如何使用位置服務。下面是我用來顯示地圖的代碼。

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<!-- The layout for the MainMap class --> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:name="com.google.android.gms.maps.SupportMapFragment"/> 

地圖活動:

public class MainMap extends FragmentActivity { 
    GoogleMap map; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_map); 


     // Get a handle to the Map Fragment 
     map = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 

     map.setMyLocationEnabled(true); 
    } 


    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (map == null) { 
      map = ((SupportMapFragment) getSupportFragmentManager(). 
        findFragmentById(R.id.map)).getMap(); 
      // Check if we were successful in obtaining the map. 
      if (map != null) { 
       // The Map is verified. It is now safe to manipulate the map. 
      } 
     }   
    } 
}//MainMap 

任何幫助表示讚賞。謝謝。

+0

http://stackoverflow.com/questions/22509861/geomapping-bearing-and-coordinate-calculation-for-googlemaps-markers/22623646#22623646這會將地圖移動到gps屏幕的中心。間隔如5000,gps和expecteddistmultiplier的更新間隔預計爲DistMultiplier = interval/6371000000.0;以米爲單位的地球半徑 – danny117

回答

1

您需要爲此添加LocationListener。

public class MainMap extends FragmentActivity{ 

public LocationManager locationManager; 
public LocationUpdateListener listener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_map); 

    // Get a handle to the Map Fragment 
    map = ((SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map)).getMap(); 

    map.setMyLocationEnabled(true); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    listener = new LocationUpdateListener();   
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
} 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (map == null) { 
     map = ((SupportMapFragment) getSupportFragmentManager(). 
       findFragmentById(R.id.map)).getMap(); 
     // Check if we were successful in obtaining the map. 
     if (map != null) { 
      // The Map is verified. It is now safe to manipulate the map. 
     } 
    }   
} 
class LocationUpdateListener implements LocationListener{ 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     // update your marker here 
    } 

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

    } 

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

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

}