开发者

How to get user's GPS coordinates by clicking a button

i want to make application like this :

User clicks the button The application show the user's coordinates (Latitude and Longitude)

i'm following the steps here

// Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          double glat = location.getLatitude();
          double glong = location.getLongitude();
          Toast.makeText(getApplicationContext(), "Your position\n"+glat+"\n"+glong, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onProviderDisabled(String provider) {}
      };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

how to implement it to button click event?

if the user clicks the button, the toast appear showing the position :)

UPDATE

I implement it using startActivityForResult() but the result is empty or null here's my code :

this is the code on the button that i want to click

btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {             
     Intent intent = new Intent(getApplicationContext(), MyLocationListener.class);
     startActivityForResult(intent, 0);
   }
});

and this is my MyLocationListener class :

public class MyLocationListener extends Activity{

    Intent intent;
    String output = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        LocationManager locationManager = (LocationManager) getApplication开发者_开发问答Context().getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                double glat = location.getLatitude();
                double glong = location.getLongitude();
                output = glat+","+glong;                
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            @Override
            public void onProviderEnabled(String provider) {}
            @Override
            public void onProviderDisabled(String provider) {}
        };

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

        intent.putExtra("returnedData", output);
        setResult(RESULT_OK, intent);
        finish();
    }

}

When i click the button it showing WTF! that means the result is null or empty.

what's the problem and what should i do then?


It wont return a result immediately since it takes time for the GPS provider to start and find your location. This is why you listen for a callback event.

Currently your code is listening for the network location, which is not very accurate. Change the LocationManager to GPS_Provider (on your last line) to use GPS if it is enabled.


Responding to an onClick is a user event, the LocationListener onLocationChanged is also an event. If you trigger the user event before the LocationListener event, there will be no position available to display.

My suggestion is to call startActivityForResult on another activity in the button onClick handler, the 2nd activity has all the LocationManager & LocationListener code in it, in the onLocationChanged event handler you pass the position back to the 1st activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜