从坐标谷歌地图Android获取地址

我正在尝试一个位置应用程序,目前我在标记上有该位置的坐标。现在,如何从已有的坐标中获取位置名称?


我的代码:


//search add address

private void geoLocate() {

    Log.d( TAG, "geoLocate: geolocating" );

    String searchString = mSearchText.getText().toString();


    Geocoder geocoder = new Geocoder( MapActivity.this );


    List<Address> list = new ArrayList<>();


    try {

        list = geocoder.getFromLocationName( searchString, 1 );

    } catch (IOException e) {

        Log.e( TAG, "geoLocate: IOException" + e.getMessage() );

    }


    if (list.size() > 0) {

        Address address = list.get( 0 );

        Log.d( TAG, "geoLocate: found a location:  " + address.toString() );

        moveCamera( new LatLng( address.getLatitude(), address.getLongitude() ), DEFAULT_ZOOM,address.getAddressLine( 0 ) );

    }

}

这是我的标记


// add marker

public void addMarkers() {

    mMap.setOnMapClickListener( new GoogleMap.OnMapClickListener() {

        @Override

        public void onMapClick(LatLng latLng) {

            MarkerOptions markerOptions = new MarkerOptions();

            markerOptions.position( latLng );

            markerOptions.title( latLng.latitude + " : " + latLng.longitude );

            mMap.clear();

            mMap.animateCamera( CameraUpdateFactory.newLatLng( latLng ) );

            mMap.addMarker( markerOptions );

            shareView();

        }

    } );

}


沧海一幻觉
浏览 121回答 1
1回答

慕哥6287543

您可以使用波纹管代码来获取基于 latlong 的地址try {&nbsp; &nbsp; &nbsp; &nbsp; Geocoder geocoder;&nbsp; &nbsp; &nbsp; &nbsp; String city=null;&nbsp; &nbsp; &nbsp; &nbsp; List<Address> yourAddresses;&nbsp; &nbsp; &nbsp; &nbsp; geocoder = new Geocoder(this, Locale.getDefault());&nbsp; &nbsp; &nbsp; &nbsp; yourAddresses = geocoder.getFromLocation(yourLatitude, yourLongitude, 1);&nbsp; &nbsp; final String yourAddress = yourAddresses.get(0).getAddressLine(0);&nbsp; &nbsp; if (yourAddresses != null && yourAddresses.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; android.location.Address address = yourAddresses.get(0);&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("MismatchedQueryAndUpdateOfStringBuilder") StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(address.getAddressLine(i)).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; city = address.getLocality();&nbsp; &nbsp; &nbsp; &nbsp; Log.d("city", "onCreate: "+city);&nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java