Professional Documents
Culture Documents
Android Google Maps Tutorial Part 3. Adding An Image To Googlemaps Using Map Overlays
Android Google Maps Tutorial Part 3. Adding An Image To Googlemaps Using Map Overlays
Pgina 1 de 8
Mir
7 Votes
Objective of this tutorial is to place an image to the current location of the device, means displaying an image at
the current coordinates from the GPS of the device. We can have a better idea from the image displayed below
of what we are trying to achieve.
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 2 de 8
A Cross image added to the current location from the GPS of the device
To start with this tutorial we will continue with the examples from the previous tutorial and to have the exact
output one must go through the previous tutorials in this series i.e,
http://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-
solved/
http://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-
gps-location-android-tutorial-part-2/
First of all we will create a PNG image with transparent background or download it from Google or search it in
the android samples folders. When we have our desired image just like the one shown below.
Our PNG
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 3 de 8
Copy this PNG and browse to your res folder of the GoogleMaps project. My path for the res folder is
C:\Users\Mir Nauman Tahir\workspace\googlemaps\res\ . In res folder we will have 3 more folders starting
from drawable i.e
1. drawable-hdpi
2. drawable-ldpi
3. drawable-mdpi
we have to copy our image in the the drawable folder but which one. If you have no idea copy the image to all
the three folders. By default the image will be taken from the hdpi folder, if the image is not found by that name
in the hdpi folder than the mdpi folder is searched. so we have to have our image in one of the two folders. If
the same image is present in both the folders than the hdpi image is taken.
The reason behind putting our image in the res folder is that we can easily access our image file by
R.id.our_image code. extension should not be entered. Using this method saves us a lot of overhead by not
getting involved in the path to the file and the addressing schemes whether relative or absolute.
Now coming to the code. First we will create our own Map Overlay Class that will
extend com.google.android.maps.Overlay. For the time being the Class should be defined inside our main
GooglemapsActivity Class. The code is given below.
We have our main map Overlay class in place, now we will come to our MyLocationListener Class. This is not
necessary here. The following code can also be defined in onCreate method of our main GooglemapsActivity
Class but as we want to draw the image on the current location of the GPS and that if the GPS coordinates
changes the image should reapear in the newly updated location we will add this code to MyLocationListener
Class.
Add the following code in the onLocationChanged(Location loc) between mc.setZoom(7) and
mapView.invalidate(). From our previous tutorial code http://mirnauman.wordpress.com/2012/02/07/using-gps-
in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 4 de 8
mapView.invalidate() should be the last statement coz it will redraw the map. Now Run the project. When the
map loads for the first time it will show no image. At this stage send dummy coordinates from the DDMS and
the map will animate to the current coordinates receieved from the DDMS and will show our Cross image on
the current location.
Sending dummy coordinates from DDMS, Map animated to the current location and displaying our "Cross"
image at the current location
This was all for adding images using map overlays. We will extend the same tutorial series for exploring further
functionalities of GoogleMaps.
Note:- Please leave your comments if this article was of any help. Thanks.
Related articles
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 5 de 8
Share this:
Like this:
26 comments
Comments feed for this article
Reply
April 16, 2012 at 9:25 am [...] Android Google Maps Tutorial Part 3. Adding An
Android Google Maps Tutorial Part Image to GoogleMaps Using Map Overlays.
6, Getting The Location That Is (mirnauman.wordpress.com) [...]
Touched. Mir Reply
Reply
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 6 de 8
June 10, 2012 at 10:48 am Since the icon is placed with its top/left
Peter Willems pixel on the geopoint location, the
overlay routine should subtract 24 pixels in
both direction of the projected x/y location on the canvas.
Reply
Reply
Reply
Reply
September 11, 2012 at 4:08 am Nice article. Can you please elaborate as
Mohsin Memon how we can place pictures from internet
directly on the map?
Reply
September 20, 2012 at 2:22 am Hi . when I use an overlay class and fix
Ziz++ error at getResource() by a Context but
when it called by activity , my map is empty ,
no map , no error .
Can u hel me
Reply
September 21, 2012 at 9:03 am check the google maps api key
Mir
Reply
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 7 de 8
Reply
Reply
September 21, 2012 at 8:30 am ur question is not clear, wat problem are
Mir u exactly facing when u load ur app on
the device
Reply
September 21, 2012 at 9:02 am u can also check the google maps api
Mir key
Reply
Reply
Reply
November 20, 2012 at 5:58 am its not that simple. this way u can get a
Mir straight line from start location to end
location. but if u really want the map to show
u the way along actual roads than u have to work really hard. google so far
not provide any api that can do this for you. but u can use any available open
source street view api
Reply
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Ma... Pgina 8 de 8
Reply
February 13, 2013 at 8:37 amI have tried twice and it seems its
Choi getting better, however, an error
message appeared Sorry! The application
Map(process com.example.map)has stopped unexpectedly. Please try again.
Can you help me please? Thanks!
Reply
February 13, 2013 at 10:42 am Are there some limitations of the image
Choi that being overlaid?
Reply
L E A V E A R E P LY
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-ma... 08/10/2013