3

GPS を使用して場所の経度と緯度を取得していますが、国、都市、州、郵便番号、番地などの場所のすべての詳細を取得したいと考えています。現在地の最大詳細。

コード:

  public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
String country;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
isNetworkEnabled = locationManager
                .isProviderEnabled
       (LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
        location = locationManager
                            .getLastKnownLocation
          (LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
        longitude = location.getLongitude();

                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
            locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
               MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  Log.d("GPS Enabled", "GPS Enabled");
      if (locationManager != null) {    
     location = locationManager.getLastKnownLocation
     (LocationManager.GPS_PROVIDER);
    if (location != null) {
    latitude = location.getLatitude();
                longitude = location.getLongitude();

                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
 }
4

4 に答える 4

6
private void initmainIntent() {

    LocationResult locationResult = new LocationResult() {

        @Override
        public void gotLocation(Location location) {
            if (location != null) {
                Geocoder gcd = new Geocoder(getApplicationContext(),
                        Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = gcd.getFromLocation(location.getLatitude(),
                            location.getLongitude(), 1);

                    if (addresses.size() > 0) {
                        String s = "Address Line: "
                                + addresses.get(0).getAddressLine(0) + "\n"
                                + addresses.get(0).getFeatureName() + "\n"
                                + "Locality: "
                                + addresses.get(0).getLocality() + "\n"
                                + addresses.get(0).getPremises() + "\n"
                                + "Admin Area: "
                                + addresses.get(0).getAdminArea() + "\n"
                                + "Country code: "
                                + addresses.get(0).getCountryCode() + "\n"
                                + "Country name: "
                                + addresses.get(0).getCountryName() + "\n"
                                + "Phone: " + addresses.get(0).getPhone()
                                + "\n" + "Postbox: "
                                + addresses.get(0).getPostalCode() + "\n"
                                + "SubLocality: "
                                + addresses.get(0).getSubLocality() + "\n"
                                + "SubAdminArea: "
                                + addresses.get(0).getSubAdminArea() + "\n"
                                + "SubThoroughfare: "
                                + addresses.get(0).getSubThoroughfare()
                                + "\n" + "Thoroughfare: "
                                + addresses.get(0).getThoroughfare() + "\n"
                                + "URL: " + addresses.get(0).getUrl();
                        locationNotFound.setVisibility(View.GONE);
                        locationFound.setVisibility(View.VISIBLE);
                        foundLocationText.setText(s);
                    }

                } catch (IOException e) {

                    e.printStackTrace();
                }

                background(location.getLatitude(), location.getLongitude());
            }
        }
    };
    MyLocation myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);
}



public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled = false;
    boolean network_enabled = false;

    public boolean getLocation(Context context, LocationResult result) {
        // I use LocationResult callback class to pass location value from
        // MyLocation to user code.
        locationResult = result;
        if (lm == null)
            lm = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);

        // exceptions will be thrown if provider is not permitted.
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = lm
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        // don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
            return false;

        if (gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                    locationListenerGps);
        if (network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                    locationListenerNetwork);
        timer1 = new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

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

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            if (location != null) {
                System.out.println(location.getLatitude());

            }
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

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

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);

            Location net_loc = null, gps_loc = null;
            if (gps_enabled)
                gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (network_enabled)
                net_loc = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            // if there are both values use the latest one
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }

            if (gps_loc != null) {
                locationResult.gotLocation(gps_loc);
                return;
            }
            if (net_loc != null) {
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult {
        public abstract void gotLocation(Location location);
    }
}
于 2012-12-25T10:58:05.173 に答える
5

これは、 GPSを使用して現在の場所と都市名を取得するためのチュートリアルです

GPS 座標を取得したら、Geocoder クラスを使用することで、すべての詳細を確認できます

以下のコードスニップは、現在の都市を取得するためのものです。他の詳細についても同じ方法で取得できます。

    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 + "\n" + latitude + "\n\nMy Currrent City is: "
                + cityName;
        editLocation.setText(s);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
} 

更新(コメントによる)

この詳細を SMS で連絡先に送信したい場合。このページが役立つと思います

于 2012-12-25T11:14:43.557 に答える
1

このコードを使用すると、通り、州、国、ピンなどの住所を見つけることができます。

Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        String address = "";
        if (addresses != null && addresses.size() >= 0) {
            address = addresses.get(0).getAddressLine(0);

            if (addresses != null && addresses.size() >= 1) {
                address += ", " + addresses.get(0).getAddressLine(1);
            }

            if (addresses != null && addresses.size() >= 2) {
                address += ", " + addresses.get(0).getAddressLine(2);
            }
        }

また、このコードはネットワーク接続を内部的に呼び出すため、別のスレッドまたは AsyncTask で実行する必要があります。

于 2012-12-25T10:58:32.850 に答える