Basic GPS problem

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

Basic GPS problem

Postby myhrvod » Sat Nov 22, 2008 2:42 pm

I write a program with gps, code is below, but when i run it on emulator, it ocurrs the error: the program stop unexpectly, i have no idea with it ,so someone can help me?
code:
Code: Select all
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidLBS extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final Button gpsButton =(Button)findViewById(R.id.gpsButton);
        gpsButton.setOnClickListener(new Button.OnClickListener(){

         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            LoadCoords();
         }

         private void LoadCoords() {
            // TODO Auto-generated method stub
            TextView latText = (TextView) findViewById(R.id.latText);
            TextView lngText = (TextView) findViewById(R.id.lngText);
            LocationManager myManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            Double latPoint = myManager.getLastKnownLocation("gps").getLatitude();
            Double lngPoint = myManager.getLastKnownLocation("gps").getLongitude();
            latText.setText(latPoint.toString());
            lngText.setText(lngPoint.toString());
         }
           
        });
    }
}


mainfest.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.myhrvod.app.gpsdemo"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidLBS"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
    </uses-permission>
</manifest>


main.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <Button
      android:id="@+id/gpsButton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Where Am I"
      />
   
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      >
      <TextView
         android:id="@+id/latLabel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Latitude: "
         />
      <TextView
         android:id="@+id/latText"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
   </LinearLayout>
   
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      >
      <TextView
         android:id="@+id/lngLabel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Longitude: "
         />
      <TextView
         android:id="@+id/lngText"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
   </LinearLayout>
</LinearLayout>
No best, Only better!
myhrvod
Freshman
Freshman
 
Posts: 7
Joined: Mon Aug 04, 2008 6:13 am

Top

Postby MrSnowflake » Sun Nov 23, 2008 1:33 pm

It probably crashes here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         gpsButton.setOnClickListener(new Button.OnClickListener(){
  2.  
  3.  
  4.  
  5.          @Override
  6.  
  7.          public void onClick(View v) {
  8.  
  9.             // TODO Auto-generated method stub
  10.  
  11.             LoadCoords();
  12.  
  13.          }
  14.  
  15.  
  16.  
  17.          private void LoadCoords() {
  18.  
  19.             // TODO Auto-generated method stub
  20.  
  21.             TextView latText = (TextView) findViewById(R.id.latText);
  22.  
  23.             TextView lngText = (TextView) findViewById(R.id.lngText);
  24.  
  25.             LocationManager myManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  26.  
  27.             Double latPoint = myManager.getLastKnownLocation("gps").getLatitude();
  28.  
  29.             Double lngPoint = myManager.getLastKnownLocation("gps").getLongitude();
  30.  
  31.             latText.setText(latPoint.toString());
  32.  
  33.             lngText.setText(lngPoint.toString());
  34.  
  35.          }
  36.  
  37.            
  38.  
  39.         });
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
The emulator doesn't have a GPS, so Android throws an exception which you don't catch.

Why didn't you look in your Log, so you know where the problem occured?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby naren.chainani » Mon Nov 24, 2008 10:12 pm

Hi,

Just to continue with this question. I had a similar problem when I ported my code from m3 to 1.0. I realized that gps was missing, but when I used the file explorer, I could see the gps provider in /data/location/gps.

Is it true that there is no built-in mock location provider in the current emulator like the one we had in the earlier sdks. If the answer is no, then even I am struggling to use this built-in location provider.

Thanks
naren
naren.chainani
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Nov 24, 2008 10:06 pm

Postby dmt » Mon Jan 05, 2009 7:14 am

Were u able to solve your GPS problem?

If not, u can follow these steps to create mock GPS data in version 1.0 :

1. create a KML file containing the sequence of latitude-longitude coordinate pairs you want to traverse
(I used the "placemarks" tool in Google Earth to generate these). Your final KML file should have the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Pos1</name>
<description>pos1</description>
<LookAt>
<longitude>-119.6569643269722</longitude>
<latitude>35.04470539956493</latitude>
<altitude>0</altitude>
<range>274067.8989160916</range>
<tilt>0</tilt>
<heading>-16.37747875059806</heading>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<coordinates>-122.1387361111111,37.46384722222223,0</coordinates>
</Point>
</Placemark>
<Placemark>
. . .
. . .
</Placemark>
:
:
:
</Document>
</kml>

and you can stack as many <Placemark> xml nodes as you like(one per coordinate pair)

2. Start the emulator and upload your KML file using the "LoadKML" button under the "KML" tab of the "Emulator Control" perspective (in Eclipse)

3. Press the 'start' arrow and your GPS motion sequence should begin

And as MrSnowflake correctly indicated, you'll need to wrap your getLastKnown().get* method calls in a try-catch block to accomodate null Location objects, then redirect your initial coordinates to some defaults.
I migrated from m5 to 1.0 and had to do some serious rewiring of my old code in order to get my apps to work with 1.0 .
User avatar
dmt
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 05, 2009 6:06 am

GPS Problem

Postby bose pandian » Tue Feb 10, 2009 7:48 am

naren.chainani wrote:Hi,

Just to continue with this question. I had a similar problem when I ported my code from m3 to 1.0. I realized that gps was missing, but when I used the file explorer, I could see the gps provider in /data/location/gps.

Is it true that there is no built-in mock location provider in the current emulator like the one we had in the earlier sdks. If the answer is no, then even I am struggling to use this built-in location provider.

Thanks
naren


hi.. please visit the following site and select wireless tutorial

Simple GPS Application Site

in this site, they are given how to write an gps application step by step..

Thanks & Regards,
Bose.C
bose pandian
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Dec 30, 2008 3:46 pm
Location: bangalore

Postby android devloper » Thu Aug 13, 2009 6:17 am

hi Bose.c

wen i run the code given int the tutorial......nothing is getting displayed.....i using 1.5 r2 version.....please gv sme suggestions...

thanks in advance.....

reagrds
android devloper
Freshman
Freshman
 
Posts: 4
Joined: Sun Jul 26, 2009 8:28 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests