Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send my GPS location (Latitude,Longitude) in a query string?

I need to make a simple application for android that send the location of my smartphone every 25 seconds to a Web-app. My Web-app is online and right now i can pass the value manually like this:

http://mywebapp.com/coordinates/create?latitude=18.463108&longitude=-69.929117

I'm an absolute beginner to Android development, so try to explain me step by step.

like image 393
mejiamanuel57 Avatar asked Mar 31 '14 14:03

mejiamanuel57


People also ask

How do I send my location latitude and longitude?

Google Maps will present a small pop-up on the bottom of the screen with your location. Pull up on that pop-up menu. Next to a marker icon you'll see the Plus Code (above your longitude and latitude.) Copy and paste that and send it to a friend to give them your current location.

How do you send latitude and longitude on Android?

Share the location if you like. Click "Share" and then choose the messaging application you would like to use. Send the message or email to yourself or to a friend. The share will include the location's latitude and longitude.


1 Answers

After doing a deep research, I found what I think is the easiest way to send GPS location (Latitude,Longitude) in a query string. Maybe there's no shorter code in the web than this. So we'll do this in 2 steps:

  1. First add this permissions to the AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  2. Now the code in your MainActivity.java inside the class public class MainActivity extends Activity { }

    public class MainActivity extends Activity {
    
        public double latitude;
        public double longitude;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            /* Use the LocationManager class to obtain GPS locations */
            LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        }
    
        /* Class My Location Listener */
    
        public class MyLocationListener implements LocationListener {
    
            @Override
            public void onLocationChanged(Location loc) {
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                String Text = "My current Latitude = " + latitude  + " Longitude = " + longitude;
                Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
    
                SendQueryString(); // for send the  Query String of latitude and logintude to the webapp.
            }
    
    
    
            @Override
            public void onProviderDisabled(String provider) {   
                Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
            }
    
    
            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
            }
    
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
    
        }/* End of Class MyLocationListener */
    
    
        public void SendQueryString() {
            new Thread() {  
                public void run() {
    
                    String url = "http://mywebapp.com/coordinates/create?latitude=" + latitude +"&longitude=" + longitude;
    
                    try {
                        HttpClient Client = new DefaultHttpClient();
                        HttpGet httpget = new HttpGet(url);
                        Client.execute(httpget);
                    }
                    catch(Exception ex) {
                        String fail = "Fail!";
                        Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
                    }
                }
            }.start();
        }
    }
    

NOTE: As you can see I'm using new Thread() { public void run() {/*code here*/}}.start(); in the function SendQueryString(), that's because we're trying to make a request to a server, so in Android you can't make a request to a server in the principal Thread, where the graphics and principal process are. In fact it'll return an error if you try to make a request in the principal Thread.

like image 157
mejiamanuel57 Avatar answered Nov 15 '22 09:11

mejiamanuel57