Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

android - 如何在Android中以编程方式获取当前GPS位置?(How do I get the current GPS location programmatically in Android?)

I need to get my current location using GPS programmatically.

(我需要以编程方式使用GPS获取当前位置。)

How can i achieve it?

(我该如何实现?)

  ask by mudit translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have created a small application with step by step description to get current location's GPS coordinates.

(我创建了一个带有逐步说明的小型应用程序,以获取当前位置的GPS坐标。)

Complete example source code is in Get Current Location coordinates , City name - in Android .

(完整的示例源代码位于Android中的“ 获取当前位置”坐标中,“城市名称”中 。)


See how it works:

(看看它怎么运作:)

  • All we need to do is add this permission in the manifest file:

    (我们需要做的就是在清单文件中添加此权限:)

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  • And create a LocationManager instance like this:

    (并创建一个LocationManager实例,如下所示:)

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
  • Check if GPS is enabled or not.

    (检查是否启用了GPS。)

  • And then implement LocationListener and get coordinates:

    (然后实现LocationListener并获取坐标:)

     LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 
  • Here is the sample code to do so

    (这是这样做的示例代码)


/*---------- Listener class to get coordinates ------------- */
private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        editLocation.setText("");
        pb.setVisibility(View.INVISIBLE);
        Toast.makeText(
                getBaseContext(),
                "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                    + loc.getLongitude(), Toast.LENGTH_SHORT).show();
        String longitude = "Longitude: " + loc.getLongitude();
        Log.v(TAG, longitude);
        String latitude = "Latitude: " + loc.getLatitude();
        Log.v(TAG, latitude);

        /*------- To get city name from coordinates -------- */
        String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
            if (addresses.size() > 0) {
                System.out.println(addresses.get(0).getLocality());
                cityName = addresses.get(0).getLocality();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        String s = longitude + "
" + latitude + "

My Current City is: "
            + cityName;
        editLocation.setText(s);
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...