Getting GPS coordinates

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Getting GPS coordinates

Postby hamorabi » Sat Sep 11, 2010 7:46 pm

Hi,

I am trying to get GPS coordinates using the following code.
The code exit with "Forced Close" - Any idea what could be the issue ?
The Manifest file permissions are:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.INTERNET"/>

Code: Select all
package com.example.SensorsAndGps;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;




public class SensorsAndGps extends Activity implements SensorEventListener {

    /** Called when the activity is first created. */
    final String tag = "SensorsAndGps";
    SensorManager sm = null;
   
    TextView xViewA = null;
    TextView yViewA = null;
    TextView zViewA = null;
    TextView xViewO = null;
    TextView yViewO = null;
    TextView zViewO = null;
    TextView gpsView = null;
    LocationManager lm;
   
   

   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lm = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
       if ( lm == null ) {
          Toast.makeText(SensorsAndGps.this, "Location Manager returned NULL", Toast.LENGTH_LONG).show();
       }
        try{
           lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000,30, (LocationListener)this);
        } catch (Exception e){
           Toast.makeText(SensorsAndGps.this, "There was an error in initialzing the GPS", Toast.LENGTH_LONG).show();
        }
        // get reference to SensorManager
        try{
           sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        } catch (Exception e){
           Toast.makeText(SensorsAndGps.this, "There was an error in initialzing the Sensors", Toast.LENGTH_LONG).show();
        }
       
        setContentView(R.layout.main);
        xViewA = (TextView) findViewById(R.id.xbox);
        yViewA = (TextView) findViewById(R.id.ybox);
        zViewA = (TextView) findViewById(R.id.zbox);
        xViewO = (TextView) findViewById(R.id.xboxo);
        yViewO = (TextView) findViewById(R.id.yboxo);
        zViewO = (TextView) findViewById(R.id.zboxo);
        gpsView = (TextView)findViewById(R.id.gpsbox);
    }
    public void onLocationChanged(Location arg0) {
        String lat = String.valueOf(arg0.getLatitude());
        String lon = String.valueOf(arg0.getLongitude());
        gpsView.setText("GPS Location: lat="+lat+"  lon="+lon);
        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
    }
    public void onProviderDisabled(String arg0) {
       Toast.makeText(SensorsAndGps.this, "Provider is disabled", Toast.LENGTH_LONG).show();
        Log.e("GPS", "provider disabled " + arg0);
    }
    public void onProviderEnabled(String arg0) {
       Toast.makeText(SensorsAndGps.this, "Provider is enabled", Toast.LENGTH_LONG).show();
        Log.e("GPS", "provider enabled " + arg0);
    }
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.e("GPS", "status changed to  " + arg0 + " [" + arg1 + "]");
    }
   
   
   
    public void onAccuracyChanged(int sensor, int accuracy) {
       Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
    }
 
    protected void onResume() {
        super.onResume();
      // register this class as a listener for the orientation and accelerometer sensors
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sm.SENSOR_DELAY_GAME);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), sm.SENSOR_DELAY_GAME);
       

    }
   
   
    protected void onStop() {
        // unregister listener
        super.onStop();
        sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
        sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));

    }
   
   public void onAccuracyChanged(Sensor arg0, int arg1) {
      // TODO Auto-generated method stub
      
   }
   
   public void onSensorChanged(SensorEvent event) {
      synchronized (this.sm) {
         switch (event.sensor.getType()){
            case Sensor.TYPE_ACCELEROMETER:
               xViewA.setText("Accel X: " + Float.toString(event.values[0]));
               yViewA.setText("Accel Y: " + Float.toString(event.values[1]));
               zViewA.setText("Accel Z: " + Float.toString(event.values[2]));
               break;
            case Sensor.TYPE_ORIENTATION:
               xViewO.setText("Accel X: " + Float.toString(event.values[0]));
               yViewO.setText("Accel Y: " + Float.toString(event.values[1]));
               zViewO.setText("Accel Z: " + Float.toString(event.values[2]));
               break;
         }
      }
    }

}
hamorabi
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 06, 2010 6:28 pm

Top

Re: Getting GPS coordinates

Postby prongs_386 » Wed Sep 15, 2010 2:17 pm

There is a problem here...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000,30, (LocationListener)this);

Your object is not a location listener, it is only a sensorEventListener. You need to implement LocationListener as well.


Apart from that I need more information.
When does it crash? As soon as you start? Does it crash with gps enabled and disabled?

The best way to see why it crashed is run the application in debug mode(Eclipse, use debug perspective) and you should see the exception that is being thrown popup. That will help you and us to understand your problem.
prongs_386
Freshman
Freshman
 
Posts: 7
Joined: Mon Sep 13, 2010 3:27 am

Re: Getting GPS coordinates

Postby pranav09 » Tue Sep 21, 2010 5:38 am

Debug your code and print the statements in LogCat from where you are getting error. It would very helpful.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 1 guest