如何从android中的地图获取地址?

时间:2014-11-12 04:51:17

标签: android google-maps

我正在构建一个Android应用程序,用户可以从地图中选择地址作为他最喜欢的地方。

我需要的是,当任何用户双击任何地方时,它应该显示在我的文本视图中,它应该以100%缩放工作。

这是我的代码 -

    package com.amal.googlemap;


public class MainActivitytut extends FragmentActivity{

GoogleMap map;
private static final LatLng GOLDEN_GATE_BRIDGE = 
        new LatLng(37.828891,-122.485884);
private static final LatLng APPLE = 
        new LatLng(37.3325004578, -122.03099823);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maintut);

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (map == null) {
        Toast.makeText(this, "Google Maps not available", 
            Toast.LENGTH_LONG).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is
    // present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.menu_sethybrid:
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        break;

    case R.id.menu_showtraffic:
        map.setTrafficEnabled(true);
        break;

    case R.id.menu_zoomin:
        map.animateCamera(CameraUpdateFactory.zoomIn());
        break;

    case R.id.menu_zoomout:
        map.animateCamera(CameraUpdateFactory.zoomOut());
        break;

    case R.id.menu_gotolocation:
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(GOLDEN_GATE_BRIDGE) // Sets the center of the map to
                                        // Golden Gate Bridge
            .zoom(17)                   // Sets the zoom
            .bearing(90) // Sets the orientation of the camera to east
            .tilt(30)    // Sets the tilt of the camera to 30 degrees
            .build();    // Creates a CameraPosition from the builder
        map.animateCamera(CameraUpdateFactory.newCameraPosition(
            cameraPosition));
        break;

    case R.id.menu_addmarker:
        // ---using the default marker---
        /*
        map.addMarker(new MarkerOptions() 
            .position(GOLDEN_GATE_BRIDGE)
            .title("Golden Gate Bridge") .snippet("San Francisco")
            .icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        */

        map.addMarker(new MarkerOptions()
            .position(GOLDEN_GATE_BRIDGE)
            .title("Golden Gate Bridge")
            .snippet("San Francisco")
            .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));
        break;

    case R.id.menu_getcurrentlocation:
        // ---get your current location and display a blue dot---
        map.setMyLocationEnabled(true);

        break;

    case R.id.menu_showcurrentlocation:
        Location myLocation = map.getMyLocation();
        LatLng myLatLng = new LatLng(myLocation.getLatitude(),
                myLocation.getLongitude());

        CameraPosition myPosition = new CameraPosition.Builder()
                .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
        map.animateCamera(
            CameraUpdateFactory.newCameraPosition(myPosition));
        break;

    case R.id.menu_lineconnecttwopoints:
        //---add a marker at Apple---
        map.addMarker(new MarkerOptions()
                .position(APPLE)
                .title("Apple")
                .snippet("Cupertino")
                .icon(BitmapDescriptorFactory.defaultMarker(
                          BitmapDescriptorFactory.HUE_AZURE)));

        //---draw a line connecting Apple and Golden Gate Bridge---
        map.addPolyline(new PolylineOptions()
            .add(GOLDEN_GATE_BRIDGE, APPLE).width(5).color(Color.RED));
        break;
    }
    return true;
 }
}

我尝试了很多方法,但没有任何方法可以满足我的需要.... 请帮忙..

3 个答案:

答案 0 :(得分:1)

试试这可能对你有帮助,

Address address = getAddressFromLatLong(context,lat,long);
public static Address getAddressFromLatLong(Context context,double lat, double lng) {

        geocoder = new Geocoder(context);

        List<Address> list = null;
        try {
            list = geocoder.getFromLocation(lat, lng, 10);
            return list.get(0);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

答案 1 :(得分:1)

首先将OnMapClickListener设置为地图。

googleMap.setOnMapClickListener(new OnMapClickListener(){
        void onMapClick(LatLng point){
            Location location = new Location("Test");
            location.setLatitude(point.latitude);
            location.setLongitude(point.longitude);
            (new GetAddressTask(this)).execute(point);
        }
    });

此处描述了GetAddressTaskhttps://developer.android.com/training/location/display-address.html#DefineTask

以下是摘录:

protected class GetAddressTask extends AsyncTask<Location, Void, String> {
    Context localContext;
    public GetAddressTask(Context context) {
        super();
        localContext = context;
    }

    @Override
    protected String doInBackground(Location... params) {
        Geocoder geocoder = new Geocoder(localContext, Locale.getDefault());
        Location location = params[0];
        List <Address> addresses = null;
        try {
            addresses = geocoder.getFromLocation(location.getLatitude(),
                location.getLongitude(), 1);
        } catch (Exception exception) {
                return "Error Message";
        }
        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);
            String addressText = ((address.getMaxAddressLineIndex() > 0) ?
                                address.getAddressLine(0) : "") +  ", " +
                                address.getLocality() + ", " +
                                address.getCountryName();
            return addressText;
        } else {
            return getString(R.string.no_address_found);
        }
    }

    @Override
    protected void onPostExecute(String address) {
        textView.setText(address);
    }
}

为了兼容Android开发者示例,我只需将LatLng对象转换为Location即可。要删除此转换,您应该将GetAddressTask类扩展为AsyncTask<LatLng, Void, String>并适应doInBackground(LatLng... latlngs)和``geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1);

答案 2 :(得分:0)

要从LAT LONG获取物理地址,请尝试此链接 -

http://android-er.blogspot.in/2011/02/get-address-from-location-using.html

在触摸事件中获得LAT LONG: -

how to get lat and long on touch event from google map?

首先点击Google地图并获取lat long,然后使用地理编码器将LatLong转换为物理地址。