Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中从经纬度获取位置的地址_Android_Google Maps_Location_Street Address - Fatal编程技术网

在android中从经纬度获取位置的地址

在android中从经纬度获取位置的地址,android,google-maps,location,street-address,Android,Google Maps,Location,Street Address,我正在构建一个应用程序。现在我得到了地图,还有被触摸位置的经纬度。我还想要与纬度和经度相对应的触摸位置的地址。我怎样才能做到这一点 public class MapViewEvents extends MapActivity { private TextView myLongitude, myLatitude; String result1; private Context context; private MapView myMapView; private MapControl

我正在构建一个应用程序。现在我得到了地图,还有被触摸位置的经纬度。我还想要与纬度和经度相对应的触摸位置的地址。我怎样才能做到这一点

 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;
    }

要从lat获得地址,long

试试这个代码

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
这是2.2版模拟器中的一个bug


这里还有:

要从lat获取地址,long

试试这个代码

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
这是2.2版模拟器中的一个bug


同样在这里:

看看我编辑的答案。如果您发现这对您有帮助,请标记为正确答案,这样对您和其他用户也有帮助。您的android sdk应用程序版本是什么?我认为您正在emulator中使用2.2。对吧?看看我编辑过的答案。如果您发现这对您有帮助,请标记为正确答案,这样对您和其他用户也有帮助。您的android sdk应用程序版本是什么?我认为您正在emulator中使用2.2。对吗?请在另一个线程或AsyncTask中执行Geocoder代码。否则它将阻止UI线程。@user370305:您能检查一下上面的错误吗?打印的错误是我已经做过的。主要错误是服务不可用。我的sdk版本是2.2。所以我没有任何方法来清除这个bug或者什么?我在更改模拟器版本时得到了正确的结果。任何人请让我知道当错误被清除。。谢谢大家…如果您想了解街道,请致电
address.getAddressLine(0)
。对于邮政编码
地址。getPostalCode()
。请在另一个线程或AsyncTask中执行地理编码器代码。否则它将阻止UI线程。@user370305:您能检查一下上面的错误吗?打印的错误是我已经做过的。主要错误是服务不可用。我的sdk版本是2.2。所以我没有任何方法来清除这个bug或者什么?我在更改模拟器版本时得到了正确的结果。任何人请让我知道当错误被清除。。谢谢大家…如果您想了解街道,请致电
address.getAddressLine(0)
。用于邮政编码
地址。getPostalCode()
   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) {}
}
java.io.IOException: Service not Available
at android.location.Geocoder.getFromLocation(Geocoder.java:117)