2012-05-03 53 views
0

我要實現我的谷歌地圖,我輸入的任何地址就會自動獲得的經度和latitude.Here是代碼我想如何從地址獲取經度和緯度?

coder = new Geocoder(this); 
try { 
    String locationName ="Gulshan 1"; 

     List<Address> addressList = coder.getFromLocationName(
       locationName, 5); 
     if (addressList != null && addressList.size() > 0) { 
      destLat = (int) (addressList.get(0).getLatitude() * 1e6); 
      destLon = (int) (addressList.get(0).getLongitude() * 1e6); 

      TextView pozycja = (TextView) findViewById(R.id.position); 
      pozycja.setText("lat: " 
        + addressList.get(0).getLatitude() + " lng: " 
        + addressList.get(0).getLongitude()); 
    } 
} 
catch (Exception e) { 
    Log.e("error", e.getMessage().toString()); 
} 

從這個代碼獲取位置是可能的嗎?

+0

請描述你做了什麼讓:「沒事」,垃圾,錯誤信息等 – Irfy

+0

它顯示「找不到0.0,0.0」 :( –

回答

2

使用下面的代碼它將工作。

Geocoder coder = new Geocoder(this); 
    List<Address> address = coder.getFromLocationName(
               "your address", 5); 
             if (address == null) 
              Log.d(TAG, 
                "Latitude and longitude not found"); 
             else { 
              Address location = address.get(0); 
              latitude = location.getLatitude(); 
              longitude = location.getLongitude(); 
             } 
+0

列表

地址address.I不能理解它。爲什麼有2次地址? –

+0

抱歉,這是我的錯誤,我將編輯..謝謝...... ... MSID –

0

你可以看到這個。這是我如何得到轉換的信息。

// getLatLon(「Gulshan-1」,「Dhaka」);

private void getLatLon(String loc,String div) { 
     Address addr = null; 
     try { 
      List<Address> addresses = new Geocoder(DataScrapper.this, Locale 
        .getDefault()).getFromLocationName(loc + " " + div 
        + " Bangladesh", 1); 

      if (!addresses.isEmpty()) { 
       addr = addresses.get(0); 

      } else 
       addr = null; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // addr.getLatitude(); 
    } 
+0

做我需要做一個新的類DataScrapper的 –

+0

?哎呀....它只是你當前班的參考 – Rasel

0
public double[] getGeoCodesFromAddress(String pLocationAddress) { 
     try { 

      cLocationGeneratorProp = new LocationGeneratorProp(); 
      objGeoCoder = new Geocoder(vContext, Locale.getDefault()); 
      vListAddress = objGeoCoder.getFromLocationName(pLocationAddress, 1); 
      if (vListAddress != null) { 
       latlong = new double[2]; 
       latlong[0] = vListAddress.get(0).getLatitude(); 
       latlong[1] = vListAddress.get(0).getLongitude(); 
      } 

     } catch (Exception e) { 
      Log.e("getGeoCodesFromAddress()", e.getMessage().toString()); 

     } 
     return latlong; 
    } 

LocationGeneratorProp

public class LocationGeneratorProp { 
    private String postalcode; 
    private String countryname; 
    private String countrycode; 
    private String fulladdress; 
    private String locationtype; 
    private double locationLat; 
    private double locationLong; 
    private double fromlocationlatitude; 
    private double fromlocationlongitude; 
    private double destinationlocationlatitude; 
    private double destinationlocationlongitude; 

    public String getPostalcode() { 
     return postalcode; 
    } 

    /** 
    * @param postalcode 
    *   Postal Code Of the Adrress 
    */ 
    public void setPostalcode(String postalcode) { 
     this.postalcode = postalcode; 
    } 

    /** 
    * @return String CountryName 
    * 
    */ 
    public String getCountryname() { 
     return countryname; 
    } 

    public void setCountryname(String countryname) { 
     this.countryname = countryname; 
    } 

    public String getCountrycode() { 
     return countrycode; 
    } 

    public void setCountrycode(String countrycode) { 
     this.countrycode = countrycode; 
    } 

    public String getFulladdress() { 
     return fulladdress; 
    } 

    public void setFulladdress(String fulladdress) { 
     this.fulladdress = fulladdress; 
    } 

    public String getLocationtype() { 
     return locationtype; 
    } 

    public void setLocationtype(String locationtype) { 
     this.locationtype = locationtype; 
    } 

    public double getLocationLat() { 
     return locationLat; 
    } 

    public void setLocationLat(double locationLat) { 
     this.locationLat = locationLat; 
    } 

    public double getLocationLong() { 
     return locationLong; 
    } 

    public void setLocationLong(double locationLong) { 
     this.locationLong = locationLong; 
    } 

    public double getFromlocationlatitude() { 
     return fromlocationlatitude; 
    } 

    public void setFromlocationlatitude(double fromlocationlatitude) { 
     this.fromlocationlatitude = fromlocationlatitude; 
    } 

    public double getFromlocationlongitude() { 
     return fromlocationlongitude; 
    } 

    public void setFromlocationlongitude(double fromlocationlongitude) { 
     this.fromlocationlongitude = fromlocationlongitude; 
    } 

    public double getDestinationlocationlatitude() { 
     return destinationlocationlatitude; 
    } 

    public void setDestinationlocationlatitude(
      double destinationlocationlatitude) { 
     this.destinationlocationlatitude = destinationlocationlatitude; 
    } 

    public double getDestinationlocationlongitude() { 
     return destinationlocationlongitude; 
    } 

    public void setDestinationlocationlongitude(
      double destinationlocationlongitude) { 
     this.destinationlocationlongitude = destinationlocationlongitude; 
    } 

} 

與給定的地址工作FNE JST測試的部份代碼讓我KNW如有prob..with部份

1

轉到與下面的代碼:

List<Address> addressList = coder.getFromLocationName(locationName, 1); 
Address addr=addressList.get(0); 
destLat=addr.getLatitude(); 
destLon =addr..getLongitude(); 
TextView pozycja = (TextView) findViewById(R.id.position); 
      pozycja.setText("lat: "+destLat+ " lng: "+destLon); 
0

您可以使用下面的代碼輕鬆地從任何地址(城市,地區,國家)獲取經度和緯度。

 public void getlatd() 

    {   
     String add=txt_add.getText().toString();//address of location u have entered 
     Toast.makeText(this, "Your add is : "+add, Toast.LENGTH_LONG).show(); 
    final Geocoder geocoder = new Geocoder(this);  
    try { 

     List<Address> addresses = geocoder.getFromLocationName(add, 1); 

     if (addresses != null && !addresses.isEmpty()) { 

     Address address = addresses.get(0); 

     txt.setText("latitude and longtd is :"+address.getLatitude()+"\n"+address.getLongitude()); 
     } 
    else { 
      Toast.makeText(this, "NO latd and longtd for this add", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch (IOException e) { 
     Toast.makeText(this, "Error :"+e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 


        "Robo-Techi"