2013-07-25 43 views
0

我是新來的android和我使用位置管理器與LocationListener和谷歌地圖API v2相關聯,我試圖得到用戶的當前位置和另一個位置之間的距離,但我總是在map.getMyLocation()上得到空指針。distanceTo(map.getMyLoctation())返回null

這裏是我的代碼:

LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
MyLocationListener locListener = new MyLocationListener(this); 
jager =(Button)findViewById(R.id.button1); 
rotebuhlplatz =(Button)findViewById(R.id.button2); 
rotebuhlst =(Button)findViewById(R.id.button3); 

if(locListener.canGetLocation){ 

    double mLat=locListener.getLatitude(); 
    double mLong=locListener.getLongitude(); 

}else{ 
    // can't get the location 
} 
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener); 


map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    Marker jager = map.addMarker(new MarkerOptions().position(DHBWJager56) 
     .title("DHBW Jägerstraße 56") 
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 



    allMarkersMap.put(jager, Jager56.class); 
    map.setOnInfoWindowClickListener(this); 

    Marker jager2 = map.addMarker(new MarkerOptions().position(DHBWJager58) 
     .title("DHBW Jägerstraße 58") 
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 
    allMarkersMap.put(jager, Jager58.class); 
    map.setOnInfoWindowClickListener(this); 
    Marker rotebuhl = map.addMarker(new MarkerOptions() 
     .position(DHBWRotebuhl) 
     .title("DHBW Rotebühlplatz 41/1").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 
    allMarkersMap.put(rotebuhl, Rotebuhl.class); 
    map.setOnInfoWindowClickListener(this); 
     // .icon(BitmapDescriptorFactory 
     // .fromResource(R.drawable.ic_launcher))); 

    Marker rts = map.addMarker(new MarkerOptions().position(DHBWRotebuhlstrasse) 
      .title("DHBW Rotebühlstraße 131")); 
    allMarkersMap.put(rts, SocialWork.class); 
    map.setOnInfoWindowClickListener(this); 
    // Move the camera instantly to Jagerstrasse with a zoom of 15. 
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(DHBWRotebuhl, 17.0f)); 

    // Zoom in, animating the camera. 
    map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null); 
    map.setMyLocationEnabled(true); 
    Location l1=new Location("source"); 
    l1.setLatitude(DHBWJager56.latitude); 
    l1.setLongitude(DHBWJager56.longitude); 
    float f=l1.distanceTo(map.getMyLocation()); 

回答

2

很高興見到您正在使用谷歌地圖API V2的工作。

這裏是谷歌地圖文檔的鏈接。 http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html

在那裏你會看到getMyLocation()的api已被棄用。他們建議您使用LocationClient。一旦你獲得了當前位置,使用相同的l1.distanceTo(locationClient.getLastLocation()),你應該很好去。希望這有助於從文檔

摘錄:

這種方法已經過時了。改用LocationClient。 LocationClient提供改進的位置查找和用電量,並由 使用「我的位置」藍點。請參閱 示例應用程序文件夾中的MyLocationDemoActivity以獲取示例代碼示例或位置開發人員指南。

示例代碼他們建議:

public class MyLocationDemoActivity extends FragmentActivity 
    implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 

    private GoogleMap mMap; 

    private LocationClient mLocationClient; 
    private TextView mMessageView; 

    // These settings are the same as the settings for the map. They will in fact give you updates at 
    // the maximal rates currently possible. 
    private static final LocationRequest REQUEST = LocationRequest.create() 
     .setInterval(5000)   // 5 seconds 
     .setFastestInterval(16) // 16ms = 60fps 
     .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_location_demo); 
    mMessageView = (TextView) findViewById(R.id.message_text); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
    setUpLocationClientIfNeeded(); 
    mLocationClient.connect(); 
    } 

    @Override 
    public void onPause() { 
    super.onPause(); 
    if (mLocationClient != null) { 
     mLocationClient.disconnect(); 
    } 
    } 

    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) { 
     mMap.setMyLocationEnabled(true); 
     } 
    } 
    } 

    private void setUpLocationClientIfNeeded() { 
    if (mLocationClient == null) { 
     mLocationClient = new LocationClient(
      getApplicationContext(), 
      this, // ConnectionCallbacks 
      this); // OnConnectionFailedListener 
    } 
    } 

    /** 
    * Button to get current Location. This demonstrates how to get the current Location as required, 
    * without needing to register a LocationListener. 
    */ 
    public void showMyLocation(View view) { 
    if (mLocationClient != null && mLocationClient.isConnected()) { 
     String msg = "Location = " + mLocationClient.getLastLocation(); 
     Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); 
    } 
    } 

    /** 
    * Implementation of {@link LocationListener}. 
    */ 
    @Override 
    public void onLocationChanged(Location location) { 
    mMessageView.setText("Location = " + location); 
    } 

    /** 
    * Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}. 
    */ 
    @Override 
    public void onConnected(Bundle connectionHint) { 
    mLocationClient.requestLocationUpdates(
     REQUEST, 
     this); // LocationListener 
    } 

    /** 
    * Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}. 
    */ 
    @Override 
    public void onDisconnected() { 
    // Do nothing 
    } 

    /** 
    * Implementation of {@link OnConnectionFailedListener}. 
    */ 
    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
    // Do nothing 
    } 
}