2015-02-05 74 views
0

我試圖從地址獲取經緯度,但它不工作無法從地址經度和緯度的Android

Geocoder geoCoder = new Geocoder(MainActivity.this);  

try { 
     List<Address> addressList = geoCoder.getFromLocationName("Salem, Tamil Nadu", 5); 
     if (addressList != null && addressList.size() > 0) { 
      double lat = addressList.get(0).getLatitude(); 
      double lng = addressList.get(0).getLongitude(); 
      Toast.makeText(MainActivity.this, " This is for the :"+lat+"long:"+lng, Toast.LENGTH_SHORT).show(); 

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

它並不顯示即使是空敬酒消息。吐司消息也不起作用。我認爲 。之後if (addressList != null && addressList.size() > 0)行什麼也沒有執行

我刪除了這個,如果條件也是它不工作。

然後addressList.size()有值爲0。甚至我給正確的地址在getFromLocationName

我試着用的答案,即使它不是從p1對象的工作,以及如何LAT和液化天然氣。敬酒消息也它不工作

Geocoder coder = new Geocoder(MainActivity.this); 
List<Address> address; 
GeoPoint p1 = null; 

try { 
    address = coder.getFromLocationName("Salem, Tamil Nadu",5); 
    if (address == null) { 

    } 
    Address location = address.get(0); 
    location.getLatitude(); 
    location.getLongitude(); 

    p1 = new GeoPoint((int) (location.getLatitude() * 1E6), 
         (int) (location.getLongitude() * 1E6)); 

    Toast.makeText(MainActivity.this, " this line works", Toast.LENGTH_SHORT).show(); 

    } 
catch(Exception ex){ 

} 

這是代碼也沒有工作,我不能看到的TextView裏的任何東西(來源)

double lat= 0.0, lng= 0.0; 

Geocoder geoCoder = new Geocoder(MainActivity.this);  
try 
{ 
    List<Address> addresses = geoCoder.getFromLocationName("Salem, Tamil Nadu, India" , 1); 
    if (addresses.size() > 0) 
    {    
     GeoPoint p = new GeoPoint(
       (int) (addresses.get(0).getLatitude() * 1E6), 
       (int) (addresses.get(0).getLongitude() * 1E6)); 

     lat=p.getLatitudeE6()/1E6; 
     lng=p.getLongitudeE6()/1E6; 

     Log.d("Latitude is:", ""+lat); 
    Log.d("Longitude is:", ""+lng); 
    source.setText(""+lat); 

    } 
} 

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

錯誤:

02-05 09:42:01.897: E/dalvikvm(1336): Could not find class 'com.google.android.maps.GeoPoint', referenced from method in.wptrafficanalyzer.locationplacesautocomplete.MainActivity$1.onClick 
02-05 09:42:01.897: W/dalvikvm(1336): VFY: unable to resolve new-instance 4019 (Lcom/google/android/maps/GeoPoint;) in Lin/wptrafficanalyzer/locationplacesautocomplete/MainActivity$1; 

所以我說

<uses-library android:name="com.google.android.maps" /> 

但如果我使用這行,然後我的仿真器不工作是這樣的

[2015-02-05 15:10:17 - LocationPlacesAutocomplete] Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY 
[2015-02-05 15:10:17 - LocationPlacesAutocomplete] Please check logcat output for more details. 
[2015-02-05 15:10:17 - LocationPlacesAutocomplete] Launch canceled! 

所以我刪除它顯示錯誤。我現在應該怎麼做 。

這是我的清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="in.wptrafficanalyzer.locationplacesautocomplete" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

    <uses-permission android:name="in.wptrafficanalyzer.locationplacesautocomplete.permission.MAPS_RECEIVE" /> 

    <permission 
     android:name="in.wptrafficanalyzer.locationplacesautocomplete.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="in.wptrafficanalyzer.locationplacesautocomplete.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 


    </application> 

</manifest> 

回答

1

這段代碼爲我工作。

public void getLatLongFromGivenAddress(String address) 
    { 
      double lat= 0.0, lng= 0.0; 

     Geocoder geoCoder = new Geocoder(this);  
     try 
     { 
      List<Address> addresses = geoCoder.getFromLocationName(address , 1); 
      if (addresses.size() > 0) 
      {    
       GeoPoint p = new GeoPoint(
         (int) (addresses.get(0).getLatitude() * 1E6), 
         (int) (addresses.get(0).getLongitude() * 1E6)); 

       lat=p.getLatitudeE6()/1E6; 
       lng=p.getLongitudeE6()/1E6; 

       Log.d("Latitude", ""+lat); 
      Log.d("Longitude", ""+lng); 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

如何獲得緯度和經度從TAT'p1' objetc – Thamaraiselvam

+0

'構造地理編碼(新View.OnClickListener(){})是未定義'誤差在'地址解析器編碼器=新地理編碼(本);'所以我改爲'MainActivity.this',但你的代碼不工作 – Thamaraiselvam

+0

緯度無法解析或不是在這一行中的字段'double x = p1.latitude;' – Thamaraiselvam