Getting coordinates without displaying maps

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

Getting coordinates without displaying maps

Postby vpachawa » Wed Oct 13, 2010 9:56 pm

Hi,

I was wondering if there is any way, where I would be able to just grab the GPS positions of the cellphone through an application, without showing the actual maps/MapActivity?

Thanks.
Appreciate your help.

Regards,
Venkata.
Regards,
Venkata Pachawa
vpachawa
Developer
Developer
 
Posts: 47
Joined: Tue Jun 08, 2010 8:54 pm
Location: St Catharines, Ontario, Canada

Top

Re: Getting coordinates without displaying maps

Postby J_R » Thu Oct 14, 2010 4:47 am

Sure, use location manager.

http://developer.android.com/reference/ ... nager.html


If you just want to grab the last known location you can just use something like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.          
  2.  LocationManager  lm=(LocationManager)context.getSystemService(LOCATION_SERVICE);
  3. Location myLoc= lm.getLastKnownLocation(NETWORK_PROVIDER)
  4. // or lm.getLastKnownLocation(GPS_PROVIDER)
  5.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I'd check that the location is recent though and if not request a location update.
http://www.staircase3.com
J_R
Developer
Developer
 
Posts: 32
Joined: Fri Jan 22, 2010 12:37 am

Re: Getting coordinates without displaying maps

Postby vpachawa » Mon Oct 18, 2010 7:55 pm

Hi

Thanks for your reply.

I tried that but didn't work for me.

It gives me null pointer exception when I try to read Lat & Long from myLoc object.
My code is
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.nr.prag;
  2.  
  3. import android.app.Activity;
  4. import android.location.Location;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.widget.Toast;
  9.  
  10. public class coordinates extends Activity {
  11.         private int lat;
  12.         private int lng;
  13.  
  14.         /** Called when the activity is first created. */
  15.         @Override
  16.         public void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 setContentView(R.layout.main);
  19.  
  20.                 LocationManager lm = (LocationManager) getApplicationContext()
  21.                                 .getSystemService(LOCATION_SERVICE);
  22.                 Location myLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  23.                 // or lm.getLastKnownLocation(NETWORK_PROVIDER)
  24.                 try {
  25.                         lat = (int) (myLoc.getLatitude() * 1E6);
  26.                         lng = (int) (myLoc.getLongitude() * 1E6);
  27.                 } catch (Exception e) {
  28.                         e.printStackTrace();
  29.                 }
  30.                 Log.i(this.getClass().getCanonicalName(), "\n\nLatitude : " + lat);
  31.                 Log.i(this.getClass().getCanonicalName(), "Longitude : " + lng);
  32.  
  33.                 Toast.makeText(getApplicationContext(),
  34.                                 "Latitude: " + lat + "\nLongitude: " + lng, Toast.LENGTH_SHORT)
  35.                                 .show();
  36.         }
  37. }
  38.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


my main.xml is
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView  
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="@string/hello"
  11.    />
  12. </LinearLayout>
  13.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


and my manifest is
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.         package="org.nr.prag" android:versionCode="1"
  4.         android:versionName="1.0">
  5.         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  6.         <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  7.         <uses-permission android:name="android.permission.INTERNET" />
  8.         <application android:icon="@drawable/icon" android:label="@string/app_name">
  9.                 <activity android:name=".coordinates" android:label="@string/app_name">
  10.                         <intent-filter>
  11.                                 <action android:name="android.intent.action.MAIN" />
  12.                                 <category android:name="android.intent.category.LAUNCHER" />
  13.                         </intent-filter>
  14.                 </activity>
  15.         </application>
  16. </manifest>
  17.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


And I have used google apis for emulator.
Do I have to use the google apis key anywhere?

Can you help me resolve this issue.

Thanks.

Appreciate your help.
Regards,
Venkata Pachawa
vpachawa
Developer
Developer
 
Posts: 47
Joined: Tue Jun 08, 2010 8:54 pm
Location: St Catharines, Ontario, Canada

Top

Return to Map Problems

Who is online

Users browsing this forum: Google [Bot] and 3 guests