[英]Cannot get latitude and longitude from address in android
I'm trying to get latitude and longitude from the address but it does not work 我正在尝试从地址获取纬度和经度,但是它不起作用
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();
}
it does not show even empty toast message . 它甚至没有显示空的吐司消息。 toast message also it not working. 吐司消息也不能正常工作。 i think . 我认为 。 after if (addressList != null && addressList.size() > 0)
line nothing executes if (addressList != null && addressList.size() > 0)
行之后没有任何执行
i removed this if condition also even it is not working. 我删除了这个条件,即使它也无法正常工作。
then addressList.size()
has value is 0. even i give correct address at getFromLocationName
然后addressList.size()
值为0。即使我在getFromLocationName
处给出正确的地址
i tried with is answer even it is not working and how to lat and lng from p1
object. 我尝试过的是答案,即使它不起作用,以及如何从p1
对象中纬度和经度。 the toast message also it not working 吐司消息也无法正常工作
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){
}
This is code is also not working i cant see anything inside the textview(Source) 这也是代码不起作用,我在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();
}
error: 错误:
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;
so i added the 所以我加了
<uses-library android:name="com.google.android.maps" />
but if i use this line then my emulator is not working is show error like this 但是,如果我使用这条线,那么我的模拟器无法正常工作,则显示如下错误
[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!
so i removed it . 所以我把它删除了。 what should i do now . 我现在应该怎么办 。
this is my manifest file 这是我的清单文件
<?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>
This snippet worked for me. 此代码段对我有用。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.