获得一个位置的地址从Android的纬度和经度

获得一个位置的地址从Android的纬度和经度

本文介绍了获得一个位置的地址从Android的纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个应用程序。在现在我收到的地图,并也被触摸的位置的纬度和经度。我还希望相应于纬度和经度的触摸位置的地址。我怎样才能做到这一点?

 公共类MapViewEvents扩展MapActivity { 私人TextView的myLongitude,myLatitude;
 串RESULT1;
 私人上下文的背景下;
 私人MapView类myMapView;
 私人MapController myMapController;
 私人双拉;
 私人双LO;
@覆盖
保护无效的onCreate(捆绑冰柱){
    // TODO自动生成方法存根
    super.onCreate(冰柱);
    的setContentView(R.layout.mymapview);    myMapView =(图形页面)findViewById(R.id.mapview);
    myMapController = myMapView.getController();
    myMapView.setBuiltInZoomControls(真);    myLongitude =(的TextView)findViewById(R.id.longitude);
    myLatitude =(的TextView)findViewById(R.id.latitude);        LA = RoamMeo_Config.Gpslatitude;
        LO = RoamMeo_Config.Gpslongitude;        INT纬度=(INT)(LA * 1000000);
        INT经度=(INT)(LO * 1000000);        GeoPoint的initGeoPoint =新的GeoPoint(经度,纬度);
        CenterLocation(initGeoPoint);        按钮nxt_button =(按钮)findViewById(R.id.btn_next);
        nxt_button.setOnClickListener(新OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    //System.out.println(result1);
                    //意向意图=新意图(Home.this,EventList.class);
                    // startActivity(意向);
            }
        });
}私人无效placeMarker(INT markerLatitude,诠释markerLongitude)
{
    可绘制标记= getResources()。getDrawable(
            android.R.drawable.ic_menu_myplaces);
    marker.setBounds(0,0,marker.getIntrinsicWidth(),
            marker.getIntrinsicHeight());
    myMapView.getOverlays()。加入(新InterestingLocations(标记,
            markerLatitude,markerLongitude));
}@覆盖
保护布尔isRouteDisplayed(){
    // TODO自动生成方法存根
    返回false;
}私人无效CenterLocation(GeoPoint的centerGeoPoint)
{
    myMapController.animateTo(centerGeoPoint);    myLongitude.setText(经度:+
        将String.valueOf((浮点)centerGeoPoint.getLongitudeE6()/ 1000000));
    myLatitude.setText(纬度:+
        将String.valueOf((浮点)centerGeoPoint.getLatitudeE6()/ 1000000));
    placeMarker(centerGeoPoint.getLatitudeE6(),
            centerGeoPoint.getLongitudeE6());
};
类InterestingLocations扩展ItemizedOverlay< OverlayItem> {    私人列表< OverlayItem>位置=
        新的ArrayList< OverlayItem>();
    私人绘制对象标记;
    私人OverlayItem myOverlayItem;    布尔MoveMap;    公共InterestingLocations(可绘制defaultMarker,
            INT LatitudeE6,诠释LongitudeE6){
        超(defaultMarker);
        // TODO自动生成构造函数存根
        this.marker = defaultMarker;
        //创建感兴趣的地点
        MyPlace酒店的GeoPoint =新的GeoPoint(LatitudeE6,LongitudeE6);
        myOverlayItem =新OverlayItem(MyPlace酒店,我的位置,我的空间);
        locations.add(myOverlayItem);        填充();
    }
    @覆盖
    保护OverlayItem createItem中(int i)以{
        // TODO自动生成方法存根
        返回locations.get(ⅰ);
    }    @覆盖
    公众诠释大小(){
        // TODO自动生成方法存根
        返回locations.size();
    }    @覆盖
    公共无效画(油画画布,MapView类MapView类,
            布尔影子){
        // TODO自动生成方法存根
        super.draw(画布,MapView类,阴影);        boundCenterBottom(标记);
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent为arg0,ARG1 MapView类){
        // TODO自动生成方法存根
        //super.onTouchEvent(arg0,ARG1);        INT行动= arg0.getAction();
        如果(动作== MotionEvent.ACTION_UP){            如果(!MoveMap)
            {
                投影PROJ = myMapView.getProjection();
                的GeoPoint LOC = proj.fromPixels((int)的arg0.getX(),(INT)arg0.getY());                //删除最后一个标记
                myMapView.getOverlays()删除(0);                CenterLocation(LOC);
            }        }
        否则,如果(动作== MotionEvent.ACTION_DOWN){            MoveMap = FALSE;        }
        否则,如果(动作== MotionEvent.ACTION_MOVE){
            MoveMap = TRUE;
        }        返回super.onTouchEvent(为arg0,ARG1);
        //返回false;
    }

我执行下面的code和我得到这个错误在我的印

logcat的:

  15 11-09:00:46.318:W / System.err的(664):java.io.IOException异常:服务不可用
11-09 15:00:46.469:W / System.err的(664):在android.location.Geo coder.getFromLocation(GEO coder.java:117)
11-09 15:00:46.469:W / System.err的(664):在com.project.r
oammeo.MapViewEvents $ 1.onClick(MapViewEvents.java:79)
11-09 15:00:46.469:W / System.err的(664):在android.view.View.performClick(View.java:2408)
11-09 15:00:46.469:W / System.err的(664):在android.view.View $ PerformClick.run(View.java:8816)
11-09 15:00:46.478:W / System.err的(664):在android.os.Handler.handleCallback(Handler.java:587)
11-09 15:00:46.488:W / System.err的(664):在android.os.Handler.dispatchMessage(Handler.java:92)
11-09 15:00:46.498:W / System.err的(664):在android.os.Looper.loop(Looper.java:123)
11-09 15:00:46.524:W / System.err的(664):在android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 15:00:46.589:W / System.err的(664):在java.lang.reflect.Method.invokeNative(本机方法)
11-09 15:00:46.641:W / System.err的(664):在java.lang.reflect.Method.invoke(Method.java:521)
11-09 15:00:46.641:W / System.err的(664):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 15:00:46.660:W / System.err的(664):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)11-09 15:00:46.660:W / System.err的(664):在dalvik.system.NativeStart.main(本机方法)


解决方案

要从纬度获得地址,长,

试试这个code,

 地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
清单<地址>地址=地理coder.getFromLocation(纬度,经度,1);

编辑:

 字符串addressString;尝试{
  地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
  清单<地址>地址=地理coder.getFromLocation(纬度,经度,1);
  StringBuilder的SB =新的StringBuilder();
  如果(addresses.size()大于0){
    地址地址= addresses.get(0);    。sb.append(address.getLocality())追加(\\ n);
    sb.append(address.getCountryName());
  }  addressString = sb.toString();  Log.e(从纬度,经度地址;addressString);
 }赶上(IOException异常五){}
}

对于异常:

  java.io.IOException异常:服务不可用
在android.location.Geo coder.getFromLocation(GEO coder.java:117)

这是在模拟器上的错误2.2

也在这里:的

I am building an application. In which now i am getting the map and also the latitude and longitude of the touched location. I also wanted the address of the touched location corresponding to the latitude and longitude. How can i achieve this?

 public class MapViewEvents extends MapActivity {

 private TextView myLongitude, myLatitude;
 String result1;
 private Context context;
 private MapView myMapView;
 private MapController myMapController;
 private double la;
 private double lo;
@Override
protected void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.mymapview);

    myMapView = (MapView)findViewById(R.id.mapview);
    myMapController = myMapView.getController();
    myMapView.setBuiltInZoomControls(true);

    myLongitude = (TextView)findViewById(R.id.longitude);
    myLatitude = (TextView)findViewById(R.id.latitude);

        la= RoamMeo_Config.Gpslatitude;
        lo=RoamMeo_Config.Gpslongitude;

        int latitude = (int)(la * 1000000);
        int longitude = (int)(lo * 1000000);

        GeoPoint initGeoPoint = new GeoPoint(latitude, longitude);
        CenterLocation(initGeoPoint);

        Button nxt_button = (Button) findViewById(R.id.btn_next);
        nxt_button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //System.out.println(result1);
                    //Intent intent = new Intent(Home.this, EventList.class);
                    //startActivity(intent);
            }
        });
}

private void placeMarker(int markerLatitude, int markerLongitude)
{
    Drawable marker=getResources().getDrawable(
            android.R.drawable.ic_menu_myplaces);
    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
            marker.getIntrinsicHeight());
    myMapView.getOverlays().add(new InterestingLocations(marker,
            markerLatitude, markerLongitude));
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private void CenterLocation(GeoPoint centerGeoPoint)
{
    myMapController.animateTo(centerGeoPoint);

    myLongitude.setText("Longitude: "+
        String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000));
    myLatitude.setText("Latitude: "+
        String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000));
    placeMarker(centerGeoPoint.getLatitudeE6(),
            centerGeoPoint.getLongitudeE6());
};


class InterestingLocations extends ItemizedOverlay<OverlayItem>{

    private List<OverlayItem> locations =
        new ArrayList<OverlayItem>();
    private Drawable marker;
    private OverlayItem myOverlayItem;

    boolean MoveMap;

    public InterestingLocations(Drawable defaultMarker,
            int LatitudeE6, int LongitudeE6) {
        super(defaultMarker);
        // TODO Auto-generated constructor stub
        this.marker=defaultMarker;
        // create locations of interest
        GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
        myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place");
        locations.add(myOverlayItem);

        populate();
    }


    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return locations.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return locations.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView,
            boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);

        boundCenterBottom(marker);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {
        // TODO Auto-generated method stub
        //super.onTouchEvent(arg0, arg1);

        int Action = arg0.getAction();
        if (Action == MotionEvent.ACTION_UP){

            if(!MoveMap)
            {
                Projection proj = myMapView.getProjection();
                GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());

                //remove the last marker
                myMapView.getOverlays().remove(0);

                CenterLocation(loc);
            }

        }
        else if (Action == MotionEvent.ACTION_DOWN){

            MoveMap = false;

        }
        else if (Action == MotionEvent.ACTION_MOVE){
            MoveMap = true;
        }

        return super.onTouchEvent(arg0, arg1);
        //return false;
    }

I executed the below code and i'm getting this error printed in my

logcat:

11-09 15:00:46.318: W/System.err(664): java.io.IOException: Service not Available
11-09 15:00:46.469: W/System.err(664):  at android.location.Geocoder.getFromLocation(Geocoder.java:117)
11-09 15:00:46.469: W/System.err(664):  at com.project.r
oammeo.MapViewEvents$1.onClick(MapViewEvents.java:79)
11-09 15:00:46.469: W/System.err(664):  at android.view.View.performClick(View.java:2408)
11-09 15:00:46.469: W/System.err(664):  at android.view.View$PerformClick.run(View.java:8816)
11-09 15:00:46.478: W/System.err(664):  at android.os.Handler.handleCallback(Handler.java:587)
11-09 15:00:46.488: W/System.err(664):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 15:00:46.498: W/System.err(664):  at android.os.Looper.loop(Looper.java:123)
11-09 15:00:46.524: W/System.err(664):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 15:00:46.589: W/System.err(664):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 15:00:46.641: W/System.err(664):  at java.lang.reflect.Method.invoke(Method.java:521)
11-09 15:00:46.641: W/System.err(664):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 15:00:46.660: W/System.err(664):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

11-09 15:00:46.660: W/System.err(664):  at dalvik.system.NativeStart.main(Native Method)
解决方案

To get the address from the lat, long,

Try this code,

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

EDIT:

   String addressString;

try {
  Geocoder geocoder = new Geocoder(this, Locale.getDefault());
  List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
  StringBuilder sb = new StringBuilder();
  if (addresses.size() > 0) {
    Address address = addresses.get(0);

    sb.append(address.getLocality()).append("\n");
    sb.append(address.getCountryName());
  }

  addressString = sb.toString();

  Log.e("Address from lat,long ;", addressString);
 } catch (IOException e) {}
}

For exception :

java.io.IOException: Service not Available
at android.location.Geocoder.getFromLocation(Geocoder.java:117)

It's a bug in the emulator for 2.2http://code.google.com/p/android/issues/detail?id=8816

also here: http://groups.google.com/group/android-developers/browse_thread/thread/b02c29d746471358

这篇关于获得一个位置的地址从Android的纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:40