2013-12-16 93 views
0

其實我使用geocoder來獲取地址..但我得到的地址值爲null.According我寫了正確的代碼來獲取當前位置從像的getAddress()和地址()方法我使用的代碼如下:獲取當前位置..在地圖上罰款..但沒有得到地址

public class CurrentLoc extends Activity { 

// latitude and longitude 
static double latitude ; 
static double longitude ; 

    // Google Map 
private GoogleMap googleMap; 
private LocationManager locationManager; 
private Location location; 
private String val; 
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.new3); 
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATES, 
       MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
       new MyLocationListener() 
     ); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener()); 

     getAddress(); 

    try { 
     // Loading map 
     initilizeMap(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

/** 
* function to load map. If map is not created it will create it for you 
* */ 

public String getAddress(){ 

     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER); 
     if (location != null) { 
      latitude= location.getLatitude(); 
      longitude= location.getLongitude(); 

      /*String message = String.format(
        "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
        lat, lng);*/ 


     try { 
      val = address(latitude, longitude); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


      Toast.makeText(CurrentLoc.this, val, 
        Toast.LENGTH_LONG).show(); 
     } 
     return val; 
    }  

    public String address(double lt,double lg) throws IOException{ 
     Geocoder geocoder; 
     List<Address> addresses; 
     geocoder = new Geocoder(this, Locale.getDefault()); 
     addresses = geocoder.getFromLocation(lt, lg, 1); 

     String address = addresses.get(0).getAddressLine(0); 
     String city = addresses.get(0).getAddressLine(1); 
     String country = addresses.get(0).getAddressLine(2); 
     return address +"\n"+ city +"\n"+ country; 
    }  






    private class MyLocationListener implements LocationListener { 

    public void onLocationChanged(Location location) { 
     String message = String.format(
       "New Location \n Longitude: %1$s \n Latitude: %2$s", 
       location.getLongitude(), location.getLatitude() 
     ); 
    // Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); 
    } 

    public void onStatusChanged(String s, int i, Bundle b) { 
     Toast.makeText(CurrentLoc.this, "Provider status changed", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderDisabled(String s) { 
     Toast.makeText(CurrentLoc.this, 
       "Provider disabled by the user. GPS turned off", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderEnabled(String s) { 
     Toast.makeText(CurrentLoc.this, 
       "Provider enabled by the user. GPS turned on", 
       Toast.LENGTH_LONG).show(); 
    } 

    } 



private void initilizeMap() { 
    if (googleMap == null) { 
     googleMap = ((MapFragment) getFragmentManager().findFragmentById(
       R.id.map)).getMap(); 

     CameraPosition cameraPosition = new CameraPosition.Builder().target(
       new LatLng(location.getLatitude(), location.getLongitude())).zoom(12).build(); 

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     MarkerOptions marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Hello Maps "); 
     googleMap.addMarker(marker); 
     googleMap.isMyLocationEnabled(); 
     // check if map is created successfully or not 
     if (googleMap == null) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! unable to create maps", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 
} 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.mnew1, menu); 

      return super.onCreateOptionsMenu(menu); 
     } 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle presses on the action bar items 
      switch (item.getItemId()) { 
       case R.id.home: 

        openSearch(); 

        return true;   
      default: 
        return super.onOptionsItemSelected(item); 
      } 
      } 

     private void openSearch(){ 

      String val1 = null; 

       val1 = getAddress(); 

      Intent intnt=new Intent(getApplicationContext(),SendSms.class); 

      intnt.putExtra("loct", val1); 
      startActivity(intnt); 
     } 

}

回答

0

移動您的代碼:

if (location != null) { 
    latitude= location.getLatitude(); 
    longitude= location.getLongitude(); 

    value=address(latitude, longitude); 

    Toast.makeText(CurrentLoc.this, value, 
    Toast.LENGTH_LONG).show(); 
} 

裏面的public void onLocationChanged(Location location)方法。

當您請求新的位置時,您不會立即得到結果。相反,您需要註冊一個監聽器,就像您在調用locationManager.requestLocationUpdates時所做的那樣,所以位置提供程序可以在爲您提取位置時通知它。

而且,從我從你的代碼的理解,你可以從呼叫locationManager.requestSingleUpdate如果你只需要在當前位置拿到手機的地址,而不是經常收到位置更新,requestLocationUpdates會給你受益。

最後,考慮這個blog post獲取獲取用戶位置的最佳實踐。如果當前位置已知,則不一定需要獲取位置更新。

希望它有幫助。

+0

我現在需要研究它..我想..u告訴了我很多東西..感謝 – user3101598

+0

@ user3101598讓我知道如果上述解決方案適合您。 –

+0

@ Ricardo ..現在面臨一個不同的問題..它顯示在地圖上的默認位置..我現在感到困惑.. – user3101598