简体   繁体   English

如何在Android中获取当前的GPS位置?

[英]How do I get the current GPS location in Android?

I have problem, I have one button and when I press button -> button.setOnclickListener -> I wil get the current GPS location . 我有问题,我只有一个按钮,当我按下button-> button.setOnclickListener->我将获得当前的GPS位置。 But when I don't press button and I want get location when my application is running, it false. 但是当我不按按钮并且我想在我的应用程序运行时获取位置时,它是错误的。 I don't understand. 我不明白 This is code for button 这是按钮的代码

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    getAdd();
}

}); });

public void getAdd()
{
    Location currentLocation = mLocationClient.getLastLocation();

           Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        //  Location location = params[0];
            List<Address> addresses = null;
            try {
                addresses = geocoder.getFromLocation(currentLocation.getLatitude(),
                        currentLocation.getLongitude(), 1);
            } catch (IOException exception1) {
                exception1.printStackTrace();
            } catch (IllegalArgumentException exception2) {
                exception2.printStackTrace();
            }
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                String addressText = context.getString(
                        R.string.address_output_string,
                        address.getMaxAddressLineIndex() > 0 ? address
                                .getAddressLine(0) : "",
                        address.getLocality(),
                        address.getCountryName());
                mAddress.setText(addressText);
            }
}

It true. 是真的。 But when i call getAdd() function when my application running. 但是,当我的应用程序运行时,我调用getAdd()函数时。 Textview mAddress.setText(addressText) false. Textview mAddress.setText(addressText)为false。 If I press button , it will true.what can I do for get my address when application is running first time? 如果按按钮,它将为true。第一次运行应用程序时,我该如何获取地址?

If I got it right, you wan the address to be displayed the first time you run the application, correct? 如果我做对了,您希望在第一次运行该应用程序时显示该地址,对吗? One way to do that is to call the getAdd() function you created in your onCreate() function, in that way, when your application runs, the address will already be displayed in your textView. 一种实现方法是调用在onCreate()函数中创建的getAdd()函数,这样,当应用程序运行时,该地址将已显示在textView中。

public class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            mMap.clear();
            if(!mProgress.isShowing())
            {
            mProgress.show();
            }
            LatLng lat=new LatLng(location.getLatitude(), location.getLongitude());
            mopt.position(lat);
            mopt.title("Loading...").snippet("").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lat,14.0f));
            myMarker=mMap.addMarker(mopt);
            myMarker.showInfoWindow();

         new ReverseAddress(getBaseContext()).execute(lat);

        }
        public void onStatusChanged(String s, int i, Bundle b) {            
        }
        public void onProviderDisabled(String s) {
        }
        public void onProviderEnabled(String s) {            
        }
    }

Use this method to get the current location also get changed current location. 使用此方法获取当前位置也会获取更改后的当前位置。 To use this code use 要使用此代码,请使用

//     Location Manager   
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                    locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, 
                                    MINIMUM_TIME_BETWEEN_UPDATE, 
                                    MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                                    new MyLocationListener()
                    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM