Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Google Maps To The Current GPS Location
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Google Maps To The Current GPS Location
Página 1 de 17
Mir
19 Votes
In this tutorial we will make our way to get Coordinates from GPS. We will use the emulator, will learn how to
use the emulator and how to use the DDMS and how we can send coordinates from DDMS to our emulator. We
will start with a very basic application that will get coordinates from the GPS and will display it in a Toast. In
the second part of this tutorial we will use those coordinates and will make our Google Maps animate to that
location automatically. For using Google Maps Basics follow the link
http://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
We will continue with the code in the above example, using the same API and AVD. First of all add the
following permission in the AndroidManifest.xml file, if its not been already added.
1 <uses-permission android:name="android.permission.INTERNET"/>
2 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
3 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Secondly check the AVD that is it with GPS support or not. As we can see in the hardware section that “GPS
Support” has a yes infront of it. Means GPS Support is installed.
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 2 de 17
There will be no change in the “main.xml” file. Now open the “GoogleMapsActivity.java” file. Add the
following to the import section of the file. At the top.
1 import android.location.Location;
2 import android.location.LocationListener;
3 import android.location.LocationManager;
1 LocationManager locMgr;
2 MyLocationListener locLstnr;
Now Create a whole new class by the name of MyLocationListner inside the GoogleMapsActivity class.
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 3 de 17
15 @Override
16 public void onProviderDisabled(String provider)
17 {
18 Toast.makeText( getApplicationContext(),
19 "Gps Disabled",
20 Toast.LENGTH_SHORT ).show();
21 }
22
23 @Override
24 public void onProviderEnabled(String provider)
25 {
26 Toast.makeText( getApplicationContext(),
27 "Gps Enabled",
28 Toast.LENGTH_SHORT).show();
29 }
30
31 @Override
32 public void onStatusChanged(String provider, int status, Bundle extras)
33 {
34
35 }
36
37 }
Now in the GoogleMapsActivity class add the following lines of code to create the LocationManager object and
MyLocationListner object.
1 locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
2 locLstnr = new MyLocationListener();
3 locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr);
Check the picture below. In the image below will have used DDMS to send dummy coordinates to our
emulator. We have to keep a few points in mind when sending dummy coordinates to emulator.
Toast displaying our dummy coordinates sent from DDMS to our emulator.
First of all emulator should be select in the Devices Tab in the DDMS. In the above picture my device is shown
as emulator-5554. Secondly while the device selected. Open the Emulator Control Tab, Inside that tab open the
Manual Tab and scroll down to location controls. Now click send and you will see a toast appear on our
emulator displaying our message including coordinates that we have just sent from DDMS. With this step our
first phase of the tutorial is complete. To animate the GoogleMap to the location of the Coordinates that we
have received. Add the following code in the onLocationChanged method of our MyLocationListner Class
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 4 de 17
4
5 GeoPoint p = new GeoPoint(
6 (int) (lat * 1E6),
7 (int) (lng * 1E6));
8
9 mc.animateTo(p);
10 mc.setZoom(7);
11 mapView.invalidate();
Comment the code that displays the Toast and run the Project. Send dummy locations from DDMS and we will
see that the Map will animate to the location of the coordinates that we have provided from DDMS.
Related articles
Android Google Maps Tutorial Part 7, Drawing A Path or Line Between Two Locations
(mirnauman.wordpress.com)
Android Google Maps Tutorial Part 1. Basic Development. (mirnauman.wordpress.com)
Android Google Maps Tutorial Part 6, Getting The Location That Is Touched. (mirnauman.wordpress.com)
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Map Overlays.
(mirnauman.wordpress.com)
Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay.
(mirnauman.wordpress.com)
About these ads
Share this:
Like this:
58 comments
Comments feed for this article
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 5 de 17
Thanks
Regards,
Martin
Reply
March 12, 2012 at 3:13 pm okey i ll soon post the complete code,
Mir but now that code is evolved to part 5, i
have not even posted tutorial for part 5. so
have no worries. i ll post the the complete code with part 5 soon. u can get the
part that is only related to GPS from that code.
Reply
Reply
Reply
Reply
March 31, 2012 at 8:58 pm Thank u very much . you are a great
soso helpfull programmer
but this code : locMgr = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locLstnr = new MyLocationListener();
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 6 de 17
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locLstnr);
Reply
and how to embed a text view to it (like option that open a text or pictures ) ?!
Reply
April 16, 2012 at 9:25 am [...] Android Google Maps Tutorial Part 2. Using GPS in
Android Google Maps Tutorial Part Android and Animating Google Maps to the Current G…
6, Getting The Location That Is (mirnauman.wordpress.com) [...]
Touched. « Mir Reply
Reply
May 2, 2012 at 4:15 am u have not posted ur xml file code. plz
Mir post that so that i can have a clear idea
of the application, 2ndly i wanted to know that
do u have only 1 activity in this application or ur have two activities. one wild
guess wud be, check the xml file, do u have a mapview control in it. also
check the manifest, do u have com.google.android.maps library added or not.
the rest we can see when u post ur xml code as well.
Reply
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 7 de 17
Reply
September 10, 2012 at 8:02 am Very nice tutorial sir i like so much.It’s
kuldeep so easy in understanding.But i need to
show path between two cities trough
walk,bus,train,car.Can you help me?
Thanks in advance
Reply
September 10, 2012 at 8:32 amso far google dont provide anything in
Mir the android sdk that can automate
drawing paths on roads etc. but u can use
Open Street Map Api to achieve that objective. so far i have not used it
myself. but planning to.
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 8 de 17
Reply
Reply
September 18, 2012 at 9:36 am Hi Mir. First of all I trouly admire your
Dave work. It helps me learning the basics of
developing to android and google API’s.
As you understand Im new at the Android apps development and I worked
according to your explanations but I couldn’t make this part works.
I added my code below. maybe youll find whats wrong.
The problem is with the initialization of LocationManager and
MyLocationListener. It dosn’t compile.
What have I done wrong?
Thanks
Dave
——
package com.test.mylocation;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
LocationManager locMgr;
MyLocationListener locLstnr;
locMgr = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
locLstnr = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locLstnr);
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Googl... Página 9 de 17
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_here);
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@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();
}
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 10 de 17
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Reply
Reply
Reply
September 21, 2012 at 8:27 am u have to create the AVD with GPS.
Mir than ur code will only work when u send
fake coordinates from ur DDMS. it will not
work by itself. u have to write the fake coordinates in the DDMS GPS section
and than click the button to send those coordinates to the emulator. than ur
map will show u those coodinates and will consider them as ur current
location
Reply
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 11 de 17
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
locMgr = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
locLstnr = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locLstnr);
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = “My current location is: ” +
“Latitud = ” + loc.getLatitude() +
“Longitud = ” + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text,
Toast.LENGTH_SHORT).show();
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 12 de 17
@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)
{
}
}
unfortunately testmap has been stopped. what did i do wrong here? reply me
ASAP pls
Reply
Reply
September 26, 2012 at 4:00 am i managed to fix it but when i run it the
Muiz map doesnt show up
September 26, 2012 at 4:10 am wat u see, an empty map grid or a black
Mir screen.
February 6, 2013 at 6:55 am I tried ur 1st and 2nd tutorials, 1st runs
tayyabajan successfuly but 2nd one displays on
emulator “unfortunately googlemaps
stopped”. it just show black screen.
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 13 de 17
Email: [email protected]
Reply
Force close
Reply
October 2, 2012 at 4:57 am plz try to print the error, in log, etc, so
Mir that we can have a clear idea wat
actually causes the application to crash
Reply
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 14 de 17
……
…..
….
….
…..
….
11 more
Reply
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 15 de 17
will you please tell me , why am i getting this. a quick response would me
much helpful sir.
Reply
Reply
October 16, 2012 at 11:04 am hello, i followed what you have told but
speed ended up with a empty grid on top of it
longitude and latitude are displayed . To get
the map below it, what should i do, can you please tell me where the error
could be ? thanks in advnce.
Reply
October 16, 2012 at 11:22 am Check your google maps Api key.
Mir
https://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-
key-for-android-issues-and-errors-solved/
Reply
But if i run only the map project without implementing GPS, it is running
well. I have checked that i am using Google API 10 2.3.3
Please help.
Reply
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 16 de 17
from implementing GPS you mean wat, which block of code are u referring
to that is crashing the app.
Reply
December 14, 2012 at 9:33 am Good day. Try your case on a real
Sergey device Samsung Galaxy Gio – Android
2.3.6, and so the map is displayed on it, and
does not show my location. The code is correct, as all of you. Help solve the
problem?
Reply
December 14, 2012 at 11:43 am please tell me wat happens when u click
Mir the my current location button
Reply
December 14, 2012 at 6:40 pm I’m sorry, I tried in the building. In open
Sergey areas all works fine. Thank you.
March 3, 2013 at 5:30 pm [...] Android Google Maps Tutorial Part 2. Using GPS in
Drawing A Path or Line Between Android and Animating Google Maps to the Current
Two Locations (Google Maps in G… (mirnauman.wordpress.com) [...]
Android) » Muhammad Yusro Reply
March 13, 2013 at 4:40 am thank you for your creat tutorial. but i
help me have a problem. it is working and appear
map in device. but is not show location . can
please put full source to download . anyone can this please send me project.
please tnx
Reply
Reply
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Go... Página 17 de 17
Reply
L E A V E A R E P LY
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-go... 08/10/2013