Search code examples
androidgpssms

How to get GPS location and send in SMS android


I can not get the GPS to work. I am saving it in a SharedPreference called gps and it will print in a Toast as "http://maps.google.com/?q=" + lat + " , " + lon this is so when it is sent in a SMS they receiver can just push the link and it opens the map. But when the toast comes up it is blank. Below is my code.

private void showLocation() {

    LocationManager locationManager = (LocationManager)
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    LocationListener loc_listener = new LocationListener() {

        public void onLocationChanged(Location l) {}

        public void onProviderEnabled(String p) {}

        public void onProviderDisabled(String p) {}

        public void onStatusChanged(String p, int status, Bundle extras) {}
    };
    locationManager
            .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
    location = locationManager.getLastKnownLocation(bestProvider);
    double lat, lon;
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }

          prefs.setPreferenceString(getApplicationContext(),"gps" ,"http://maps.google.com/?q=" + lat +  " , " + lon);



}

Solution

  • to find your location try to use GPSTracker. link

    And use this code.

            gps = new GPSTracker(LauncherActivity.this);
            latitude = gps.getLatitude();
            longitude = gps.getLongitude();
    

    And one more things, check your device, is it support GPS? And is it (GPS) enable? If your device's GPS is not enable, you will get null....

    For more help... check my github repo