当我有位置的经度和纬度时,我试图从android中的谷歌地图api中找出位置名称。我想要实现的是,在获得位置名称后,我想向我的朋友发送短信,告诉他们我当前的位置。我不确定是否需要打开地理编码器服务。
这是我到目前为止所做的,现在我被卡住了。
class mylocationlistener implements LocationListener
{
@SuppressLint("NewApi")
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null)
{
Double longi = location.getLongitude();
Double lat = location.getLatitude();
String str = "";
str= "Longitude=" + longi + ", Latitude=" + lat;
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
boolean abc = Geocoder.isPresent();
try {
List<Address> addresses = geocoder.getFromLocation(lat, longi, 1);
if(!addresses.isEmpty())
{
str = addresses.get(0).getLocality() + addresses.get(0).getAddressLine(1)+ addresses.get(0).getAddressLine(2);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),str, Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
我认为你差不多完成了它,我在使用以下代码之前如何解决这个问题:
public static String getAddressStringFromLocation(Context context, Location location) {
GeoPoint gp = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
return getAddressStringFromGeoPoint(context, gp);
}
public static String getAddressStringFromGeoPoint(Context context, GeoPoint point) {
StringBuilder sb = new StringBuilder();
Address ad = getAddressFromGeoPoint(context, point);
if(ad != null && ad.getMaxAddressLineIndex() > 0) {
for(int i = 0, max = ad.getMaxAddressLineIndex(); i < max; i++) {
sb.append(ad.getAddressLine(i));
}
sb.append(ad.getThoroughfare());
return sb.toString();
} else {
return null;
}
}
public static Address getAddressFromGeoPoint(Context context, GeoPoint point){
Geocoder geoCoder = new Geocoder(context, Locale.CHINA);
Address address = null;
try {
List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1);
address = addresses.get(0);
} catch (IOException e) {
e.printStackTrace();
}
return address;
}