我正在尝试显示当前的情况和经度,纬度。我可以轻松获取经度和纬度但不能显示当前位置。 我正在下面显示我的代码。
public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager ;
String provider;
Context context;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if(location!=null)
{
onLocationChanged(location);
//Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onLocationChanged(final Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);
// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );
// From here i want to get the location, as i pass current longitude &
这里的纬度。它传递null 在地址中意味着它执行else而不是if。
try {
Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null ) {
Address address = addresses.get(0);
// sending back first address line and locality
String result = address.getAddressLine(0) + ", " + address.getLocality();
Toast.makeText(getApplicationContext(),"result",Toast.LENGTH_SHORT).show();
}
if (addresses.size() > 0) {
String address = "";
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
Log.v("AddressTag", address);
}
else
{
Toast.makeText(getApplicationContext(),"Hello2",Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
如果List为空/ null我敢打赌你没有Geocode后端服务来进行实际的查找。 documentation says“Geocoder类需要一个未包含在核心android框架中的后端服务。如果平台中没有后端服务,Geocoder查询方法将返回一个空列表。”
如果您想使用Google Maps API进行反向查询,则需要include it in your project和obtain a Maps API key。 “在以前版本的SDK中,com.google.android.maps包已包含在标准的Android库和系统映像中。在Android 1.5 SDK中,情况并非如此.Android 1.5库和系统映像不包括在内地图外部库(com.google.android.maps)。但是,地图外部库可作为Android SDK的Google API插件的一部分提供“
更新:由于您提到GeoCoder.isPresent()返回true,这意味着后端服务存在且问题出在其他地方。我查看了Google Maps API常见问题解答,并提到了lat / long来解决查找问题doesn't work everywhere。查看this spreadsheet并将地理编码过滤器设置为否。您正在查找列出的国家/地区?
答案 1 :(得分:0)
您确定Geocoder
正在使用您的设备吗?这是他们在Geocoder的描述中指定的内容。
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
这只适用于一些设备,如果你真的想获得你应该使用google api的地址。 https://developers.google.com/maps/documentation/geocoding/
答案 2 :(得分:0)
您是否已在清单中提供这些权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
您的设备是否已连接到互联网?
使用Geocoder.isPresent()检查是否存在地理编码器