【问题标题】:How can I access my current location in onLocationChanged, in getMyLocationAddress method?如何在 getMyLocationAddress 方法中访问我在 onLocationChanged 中的当前位置?
【发布时间】:2017-03-27 05:28:17
【问题描述】:

我想在 getMyLocationAddress 的 onLocationChanged 方法中访问我当前的位置来设置文本框的文本。我想知道我的位置准确存储在哪个变量中,以便我可以在 geocoder.getLocationFrom() 中使用它。

我在 geocoder.getFromLocation(latLng.getLatitude(),latLng.getLongitude(),1) 中使用 LatLng 对象 latLng; 我猜这是存储当前位置的位置。但是 getFromLocation 需要 Location 对象。我应该使用哪个 Location 对象作为 LatLng 对象显然会出错?

@Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(50));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }


 public void getMyLocationAddress() {

    Geocoder geocoder= new Geocoder(this, Locale.ENGLISH);

    try {

        //Place your latitude and longitude
       //List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);
        List<Address> addresses = geocoder.getFromLocation(latLng.getLatitude(),latLng.getLongitude(),1);

        if(addresses != null) {

            Address fetchedAddress = addresses.get(0);
            StringBuilder strAddress = new StringBuilder();

            for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
            }

            etOrigin.setText(strAddress.toString());

        }

        else
            etOrigin.setText("No location found..!");

    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),"Could not get address..!", Toast.LENGTH_LONG).show();
    }
}

【问题讨论】:

    标签: android google-maps android-studio android-location currentlocation


    【解决方案1】:

    你需要使用Location类型的变量。

    您可以在此处阅读更多内容: https://developer.android.com/reference/android/location/Location.html

    【讨论】:

    • 是的,但是我的当前位置存储在哪个位置变量中
    • 您的 latLng 变量是局部变量。它只能在 onLocationChanged() 方法中访问。全局声明它,以便可以在 getMyLocationAddress() 中访问它。
    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多