2016-02-05 149 views
0

我想從經度和緯度獲取地址,但無法獲取地址。我得到隆吉。和lati。值,但是當我將它傳遞給getAddress的函數時,它會停止工作請幫助我,如果你們可以的話。 這裏是我的代碼使用經度和緯度無法獲取地址Android

MainActivity.java文件

package com.example.mygps; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import android.app.Activity; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 


public class MainActivity extends Activity { 

    Button btnGet; 
    GPS_Class gps; 
    TextView adr,cty,ctry; 

    double longi, lati; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btnGet = (Button)findViewById(R.id.btnGo); 
     adr = (TextView)findViewById(R.id.adr); 
     cty = (TextView)findViewById(R.id.cty); 
     ctry = (TextView)findViewById(R.id.ctry); 

     btnGet.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       gps = new GPS_Class(MainActivity.this); 
       if(gps.canGetLocation()) 
       { 

        longi = gps.getLongitude(); 
        lati = gps.getLatitude(); 
        Toast.makeText(MainActivity.this, "Longitude is:"+longi+"Latidute is:"+lati, Toast.LENGTH_LONG).show(); 
        getAddress(longi, lati); 
       } 
       else 
       { 
        gps.showSettingsAlert(); 

       } 
      } 
     }); 

    } 

    public void getAddress(double longitude, double latitude) 
    { 
     double long1,lati1; 
     long1 = longitude; 
     lati1 = latitude; 
     if(lati>0 && long1>0) 
     { 
     Geocoder geocode = new Geocoder(this, Locale.getDefault()); 
     List<Address> addresses; 

     try { 
      addresses = geocode.getFromLocation(latitude,longitude, 1); 

      String Addres_ = addresses.get(0).getAddressLine(0); 
      String Country = addresses.get(0).getCountryName(); 
      String City = addresses.get(0).getLocality(); 

      adr.setText(Addres_); 
      cty.setText(City); 
      ctry.setText(Country); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
     }//if closing. . . 
     else 
     { 
      Toast.makeText(this, "No Vlaue", Toast.LENGTH_LONG).show(); 
     } 


    } 
} 

我GPS_Class.java文件代碼

package com.example.mygps; 

import android.app.AlertDialog; 
import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 

public class GPS_Class extends Service implements LocationListener{ 



    //To Get Context of the class... 
    Context context; 

    //Declaring Variable to use. . . 
    double lattitude; 
    double longitude; 

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 

    boolean isGPSEnabled = false; 
    boolean isNetWorkEnabled = false; 
    boolean canGetLocation = false; 

    //Declaring objects of different classes... 
    Location location; 
    LocationManager locationmanager; 

    public GPS_Class(Context context) { 
     this.context = context; 
     GetLocation(); 
    } 

    //Self Coded Function to perform all location works . . . 
    private Location GetLocation() 
    { 
     locationmanager = (LocationManager)context.getSystemService(LOCATION_SERVICE); 

     isGPSEnabled = locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     isNetWorkEnabled = locationmanager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if(isNetWorkEnabled) 
     { 
      canGetLocation = true; 
      locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this); 

      if(locationmanager !=null) 
      { 
       location = locationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if(location !=null) 
       { 
        lattitude = location.getLatitude(); 
        longitude = location.getLongitude(); 
       } 
      } 
     } 

     if(isGPSEnabled) 
     { 
      canGetLocation = true; 
      if(location == null) 
      { 
      locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this); 
      if(locationmanager != null) 
      { 
       location = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

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

     return location; 
    } 

    public void showSettingsAlert(){ 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 

      // Setting Dialog Title 
      alertDialog.setTitle("GPS is settings"); 

      // Setting Dialog Message 
      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

      // On pressing Settings button 
      alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int which) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        context.startActivity(intent); 
       } 
      }); 

      // on pressing cancel button 
      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
       } 
      }); 

      // Showing Alert Message 
      alertDialog.show(); 
     } 


    public double getLatitude(){ 
      if(location != null){ 
       lattitude = location.getLatitude(); 
      } 

      // return latitude 
      return lattitude; 
     } 

    public double getLongitude() 
    { 
     if(location !=null) 
     { 
     longitude =location.getLongitude(); 
     } 
     return longitude; 
    } 

    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 
    @Override 
    public void onLocationChanged(Location arg0) { 
     // TODO Auto-generated method stub 

    } 

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

    } 

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

    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 


} 
+0

你是什麼意思**停止工作**在這裏? – Rehan

+0

@Rehan如果我會評論getAddress();功能它會給我龍吟。拉蒂。但是,如果我將取消註釋我的應用程序停止工作的功能。 –

+0

究竟發生了什麼?該應用程序崩潰或進入沒有響應的狀態?或者停止工作,你的意思是它沒有按照你的期望工作? – Rehan

回答

1

得到的解決方案 上述提到張貼co de很好它可以獲取當前位置。 如果您嘗試在模擬器上運行它,它會向您顯示錯誤,因爲「Geocoder」類與Emulator不兼容。 在設備上運行它工作正常。

0

嘗試與getAddressMainActivity.this改變this

Geocoder geocode = new Geocoder(MainActivity.this, Locale.getDefault()); 

getFromLocation方法拋出。所以檢查你的緯度值。它不在正確的範圍內:

IllegalArgumentException if latitude is less than -90 or greater than 90 

IllegalArgumentException if longitude is less than -180 or greater than 180 

IOException if the network is unavailable or any other I/O problem occurs 
+0

沒有兄弟不工作。 –

+0

你能詳細說明你得到了什麼錯誤嗎? –

+0

只是應用程序停止與該不幸的應用程序無法正常工作。 –

0

實際功能是getFromLocation(double latitude, double longitude, int maxResults)。您將通過longitude代替latitudelatitude代替longitude。嘗試改變這一行:

addresses = geocode.getFromLocation(longitude, latitude, 1); 

這樣:

addresses = geocode.getFromLocation(latitude, longitude, 1); 

,並添加空和尺寸檢查數組訪問它之前,以避免NullPointerExceptionIndexOutOfBoundExceptiondocumentation說,它可以返回空值陣列

if (addresses != null && addresses.size() > 0) { 
    String Addres_ = addresses.get(0).getAddressLine(0); 
    String Country = addresses.get(0).getCountryName(); 
    String City = addresses.get(0).getLocality(); 

    adr.setText(Addres_); 
    cty.setText(City); 
    ctry.setText(Country); 
} else { 
    // Reset fields here 
}