Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:28:46+00:00 2026-05-27T22:28:46+00:00

I have looked about and cannot find any direct threads regarding what I am

  • 0

I have looked about and cannot find any direct threads regarding what I am looking for. I am trying to create an Android application which dials out an emergency number at the push of a button (which I have got working) but cannot get the location (displayed in Longitude and Latitude) to display, I have tried doing it with Toast and EditText boxes. I am new to Android development so wanted to start with something easy, but the LongLat part is being troublesome. Any help would be greatly appreciated.

Below is the code I have been tampering with in order to try and get it to grab the Long and Lat, then in another file I have been trying to use a click listener to assign it to a button so when the button is pressed (in main.xml) it will display the Long and Lat either in a textfield or in toast.

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;



        public class EmergencyLocation extends Activity implements LocationListener {
            private TextView latituteField;
            private TextView longitudeField;
            private LocationManager locationManager;
            private String provider;

            /** Called when the activity is first created. **/
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                latituteField = (TextView) findViewById(R.id.TextView);
                longitudeField = (TextView) findViewById(R.id.long_lat);

                // Get the location manager
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                // Define the criteria how to select the location provider -> use
                // default
                Criteria criteria = new Criteria();
                provider = locationManager.getBestProvider(criteria, false);
                Location location = locationManager.getLastKnownLocation(provider);

                // Initialise the location fields
                if (location != null) {
                    System.out.println("Provider " + provider + " has been selected.");
                    int lat = (int) (location.getLatitude());
                    int lng = (int) (location.getLongitude());
                    latituteField.setText(String.valueOf(lat));
                    longitudeField.setText(String.valueOf(lng));
                } else {
                    latituteField.setText("Provider not available");
                    longitudeField.setText("Provider not available");
                }
            }








        private void TextView() {
            // TODO Auto-generated method stub

        }


        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub

        }


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

        }


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

        }


        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:28:47+00:00Added an answer on May 27, 2026 at 10:28 pm

    First, you need to set up a LocationManager:

    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    
    // set preferred provider based on the best accuracy possible
    Criteria fineAccuracyCriteria = new Criteria();
    fineAccuracyCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    String preferredProvider = manager.getBestProvider(fineAccuracyCriteria, true);
    

    Now, you have to create a LocationListener. In this case, it calls the method updateLocation():

    LocationListener listener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // called when a new location is found by the network location provider.
                updateLocation(location);
            }
    
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    
            public void onProviderEnabled(String provider) {}
    
            public void onProviderDisabled(String provider) {}
        };
    

    EDIT:

    Then, you have to register the listener with your LocationManager (and try to get the cached location):

    manager.requestLocationUpdates(preferredProvider, 0, 0, listener);
    // get a fast fix - cached version
    updateLocation(manager.getLastKnownLocation());
    

    And finally, the updateLocation() method:

    private void updateLocation(Location location) {
        if (location == null)
            return;
    
        // save location details
        latitude = (float) location.getLatitude();
        longitude = (float) location.getLongitude();        
    }
    

    EDIT2:

    OK, just saw your code. In order to make it work, just move around a few bits:

    /** Called when the activity is first created. **/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView);
        longitudeField = (TextView) findViewById(R.id.long_lat);
    
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the location provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        locationManager.requestLocationUpdates(provider, 0, 0, this);
        Location location = locationManager.getLastKnownLocation(provider);
        onLocationChanged(location);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(this);
    }
    
    @Override
    public void onLocationChanged(Location location) {
       if (location != null) {
           System.out.println("Provider " + provider + " has been selected.");
           int lat = (int) (location.getLatitude());
           int lng = (int) (location.getLongitude());
           latituteField.setText(String.valueOf(lat));
           longitudeField.setText(String.valueOf(lng));
       } else {
           latituteField.setText("Provider not available");
           longitudeField.setText("Provider not available");
       }
    }
    

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked into this and cannot find a suitable answer. I want to
I have looked on SO but cannot find an answer that works for me,
I have looked at the documentation on MSDN about these 2 functions. However, I
I have looked on FaceBook Developer page and found that it's possible to create
I have looked around on the Internet trying to answer this question. It seems
Right now i am trying to learn more about java threading, and i have
I have got this problem: Find the first element in a list, for which
I have looked around the internet for 3 hours now looking for a solution
I have looked through these forums to find a solution to this problem, and
I have looked around and I cannot figure out how to do it. Should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.