我试图从地址获取经度和经度,但它不起作用
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();
}
它甚至不显示空的Toast消息。吐司的消息也没有用。我认为 。在if (addressList != null && addressList.size() > 0)
行之后没有执行
我删除了这个条件,即使它不起作用。
然后addressList.size()
的值为0.即使我在getFromLocationName
我试着回答即使它不起作用以及如何从p1
对象进行拉取和lng。吐司的消息也不起作用
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(Source)
中看到任何内容 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>
答案 0 :(得分: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();
}
}
}