To get going with this tutorial one needs to go through all the previous 3 parts because we are using the same code from the previous tutorials and taking it a few steps further.
- https://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
- https://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
- https://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/
Our main emphasis in this tutorial will be on adding a Menu to our Android GoogleMaps project and perform different actions using the buttons of that menu. So wat we will do is to create a menu with 3 buttons. Just like the one shown in the image below.
- My Current Location Button
- Map Satellite View Button
- Map Normal View Button
Before we start we will copy 3 images(mylocation.png,satelliteview.png,normalview.png) in the drawables folder in the res folder. After this we will create a new xml file in the layout folder. We will name our file as “menu.xml”. The code for menu.xml is given below.
Save the menu.xml file and open GooglemapsActivity.java file. Now add the following code after the onCreate method in the GooglemapsActivity Class.
// Initiating Menu XML file (menu.xml) @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.layout.menu, menu); return true; } /** * Event Handling for Individual menu item selected * Identify single menu item by it's id * */ @SuppressWarnings("deprecation") @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.my_location: // Single menu item is selected do something Toast.makeText(GooglemapsActivity.this, "Moving To Current location", Toast.LENGTH_SHORT).show(); locLstnr.gpsCurrentLocation(); return true; case R.id.mapview_normal: Toast.makeText(GooglemapsActivity.this, "Map Normal Street View", Toast.LENGTH_SHORT).show(); if(mapView.isSatellite()==true){ mapView.setSatellite(false); } return true; case R.id.mapview_satellite: Toast.makeText(GooglemapsActivity.this, "Map Satellite View", Toast.LENGTH_SHORT).show(); if(mapView.isSatellite()==false){ mapView.setSatellite(true); } return true; default: return super.onOptionsItemSelected(item); } }
In the above code, i have used locLstnr.gpsCurrentLocation(). To follow this completely one has to go through all the 3 previous parts of this tutorial. I have created a user defined function by the name of gpsCurrentLocation inside MyLocationListener Class. In this function I am getting the current coordinates and animating the map to that location. In the above code I have used the menu button to initiate this function. Now when we click the “My Location” button, the map will be animated to the current location, if we click the “Satellite View” button the map will be converted to satellite view and if we click the “Normal View” button than the map will be converted to Normal View.
Run and test the project and it will give us the following out put.
Now coming to the zooming functionality. All we need is just one line of code in the GooglemapsActivity Class. Add the following line after mapView = (MapView) findViewById(R.id.mapview1); in the onCreate method of our main GooglemapsActivity Class.
mapView.setBuiltInZoomControls(true);
Run the project and click on the map, click is must. As soon as the map is clicked the zooming controls will appear on the map.
Complete Source of MyLocationListener Class
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(); tvlat.setText(""+loc.getLatitude()); tvlong.setText(""+loc.getLongitude()); this.gpsCurrentLocation(); } public void gpsCurrentLocation() { String coordinates[] = {""+tvlat.getText(), ""+tvlong.getText()}; double lat = Double.parseDouble(coordinates[0]); double lng = Double.parseDouble(coordinates[1]); GeoPoint p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6)); mc.animateTo(p); mc.setZoom(7); MyMapOverlays marker = new MyMapOverlays(p) ; List listOfOverLays = mapView.getOverlays(); listOfOverLays.clear(); listOfOverLays.add(marker); mapView.invalidate(); } @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) { } }
Complete code of GooglemapsActivity Class
public class GooglemapsActivity extends MapActivity { private MapView mapView; private MapController mc; private TextView tvlat; private TextView tvlong; LocationManager locMgr; MyLocationListener locLstnr; Location mloc; /** Called when the activity is first created. * @return */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.mapview1); mc = mapView.getController(); mapView.setBuiltInZoomControls(true); tvlat = (TextView)findViewById(R.id.tv_lat); tvlong = (TextView)findViewById(R.id.tv_long); tvlat.setText("0"); tvlong.setText("0 "); locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locLstnr = new MyLocationListener(); locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr); } // Initiating Menu XML file (menu.xml) @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.layout.menu, menu); return true; } /** * Event Handling for Individual menu item selected * Identify single menu item by it's id * */ @SuppressWarnings("deprecation") @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.my_location: // Single menu item is selected do something Toast.makeText(GooglemapsActivity.this, "Moving To Current location", Toast.LENGTH_SHORT).show(); locLstnr.gpsCurrentLocation(); return true; case R.id.mapview_normal: Toast.makeText(GooglemapsActivity.this, "Map Normal Street View", Toast.LENGTH_SHORT).show(); if(mapView.isSatellite()==true){ mapView.setSatellite(false); } return true; case R.id.mapview_satallite: Toast.makeText(GooglemapsActivity.this, "Map Satallite View", Toast.LENGTH_SHORT).show(); if(mapView.isSatellite()==false){ mapView.setSatellite(true); } return true; default: return super.onOptionsItemSelected(item); } } }
Note:- Please leave your comments if this article was helpful.
Update # 1:- In response to Max Comment # 1:-
Dear Max,
ur confusion is right coz i have slightly changed the logic in the MyLocationListener Class and have not posted the Updated code.. The target was to click a button and move to the current location of the GPS. But it was not that simple. So here is wat i did.
In the onLocationChanged() method in MyLocationListener Class i assigned the Latitude and Longitude to two hidden textviews.
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(); tvlat.setText(“”+loc.getLatitude()); tvlong.setText(“”+loc.getLongitude()); }
Than i created a new method inside MyLocationListener Class i.e, gpsCurrentLocation(). In gpsCurrentLocation() method i have used the code to get the coordinates from the textviews and animate the map to that location.
public void gpsCurrentLocation() { String coordinates[] = {“”+tvlat.getText(), “”+tvlong.getText()}; double lat = Double.parseDouble(coordinates[0]); double lng = Double.parseDouble(coordinates[1]); GeoPoint p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6)); mc.animateTo(p); mc.setZoom(7); MyMapOverlays marker = new MyMapOverlays(p) ; List listOfOverLays = mapView.getOverlays(); listOfOverLays.clear(); listOfOverLays.add(marker); mapView.invalidate(); }
After this call the gpsCurrentLocation() method in the OnLocationChanged() too . so that the method becomes like this
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(); tvlat.setText(“”+loc.getLatitude()); tvlong.setText(“”+loc.getLongitude()); this.gpsCurrentLocation(); }
Now come to the code of ur button click, or in my case menu click.
Create an object of MyLocationListener Class.
and simply call the the gpsCurrentLocation() method through that object. and u will be able to animate the map to the current GPS coordinates.
MyLocationListener locLstnr = new MyLocationListener();
locLstnr.gpsCurrentLocation();
Plz leave ur comments if this solves the issue of animating the map to the current GPS coordinates on button click or menu click.
Update # 2: In response to Sagar Zade issue.
Related articles
- Android Google Maps Tutorial Part 6, Getting The Location That Is Touched. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 1. Basic Development. (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 2. Using GPS in Android and Animating Google Maps to the Current GPS location. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 7, Drawing A Path or Line Between Two Locations (mirnauman.wordpress.com)
68 comments
Comments feed for this article
February 15, 2012 at 11:21 pm
Max
Hey,
nice tut. I was looking for an “gpsCurrentLocation” function inside the MyLocationListener – Class – buy can’t find it. I created one but it wants some data in the constructor. Actually I thought this code works like a button so it just calls the function.
Can you give me a hint to solve this?
Thanks,
Max
February 16, 2012 at 5:27 am
Mir
Dear Max,
ur confusion is right coz i have slightly changed the logic in the MyLocationListener Class and have not posted the Updated code.. The target was to click a button and move to the current location of the GPS. But it was not that simple. So here is wat i did.
plz check Update#1 at the end of this article.
February 20, 2012 at 10:14 pm
Max
Hey Mir,
thanks for your very quick answer. I understood what you were doing and it’s a smart idea!
It almost works for me. I have create a view in the “main.xml” to adress the ID’s for “tv_lat” and “tv_long”. I did this, but then I always get error messages.
Do I have to create multiple android.maps.MapViews or is one just enough?
Thanks for your help!
Max
February 20, 2012 at 10:48 pm
Max
Okay got it. I inserted the following in the main.xml :
And the same for the other textview.
Do I have to set any text? I don’t think so!
I tried this application this way and worked really fine!
What will you be doing next in this tutorial? I think it would be interested to see multiple markers like many your “red cross” image. Just the same google does, when showing search results in google maps. I guess the must be some API and a Database to feed the App with locations?
Looking forward to hear from you!
Max
February 21, 2012 at 4:40 am
Mir
Dear Max,
u dont need multiple mapviews. one will do the job. secondly its better to initialize the textviews as i have provided lat = 0 and long = 0. later on the app can set the texviews text from the GPS or even the DDMS. n ya u r right i have already started working on my next tutorial. that will cover multiple overlays. adding multiple images on the map and getting the location that is touched. so we can either add multiple images by touching multiple locations on the screen one by one. or can get lat,long sets from a kml file. But currently am planning to go for the multiple overlays First.
n Thanks for Appreciating my efforts.
February 25, 2012 at 5:01 pm
Gizem
Hi,
Actually I can not get the current location data.I mean I want to get the location data where I am (exactly where my android device is).
In this tutorial we get the data by emulator.When I load the apk on an android device,I cannot get the real location data.
Can you help me,how to get it?
Thank you
March 6, 2012 at 5:08 am
Mir
well i dont have a real android device so i have not encountered this scenario yet. can u check ur gps from device. if its enable or disabled. coz in ideal situation this code is supposed to work wid actual device. still i try to manage a real device and try the code on it. if its giving me the same bug than i figure out wat could be the real problem.
March 12, 2012 at 5:30 pm
Emma
GReat Tut, will there be more?
March 13, 2012 at 3:47 am
Mir
thanks emma for appreciating my work. ya there will be more. i ll soon publish part 5 in this series. with the complete code
April 10, 2012 at 12:15 pm
Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay. « Mir
[…] https://mirnauman.wordpress.com/2012/02/14/android-google-maps-tutorial-part-4-adding-menu-some-addit… […]
April 13, 2012 at 9:25 am
Android Development
Hello Dear i have developed an application but that is not showing my current location. and i also wanted to get address of a point but it is also not working. i can show you all my coding. Would you like to help me in solving my problem. I stuck in this problem 2 weeks ago but still not found any reasonable solution.
April 17, 2012 at 5:16 am
Mir
i have examined ur code,
if u want to get ur current location, u need to use GPS, configure GPS in the emulator.
2ndly ur onLocationChanged() event is empty. to get something from the GPS u have to fire this at least once. get the coordinates inside onLocationChanged() than provide dummy coordinates though DDMS. ur map will take u to the current location. as the emulator will consider the coordinates from the DDMS as ur current location that is provided by an actual GPS.
For reference check the following tutorial
April 18, 2012 at 8:44 am
Android Development
I have tried my best and i am again sending my code here. But still no progress.
I have been facing this problem since 1 month ago and now i am stuck at this point and not moving further.
Please solve my problem.
April 30, 2012 at 1:44 pm
Android Development
Please reply to this Post:
http://learnglobally.wordpress.com/2012/04/30/problem-using-android-mapactivity/
April 16, 2012 at 6:18 am
Mir
yes i wud like to help but u need to give it a try first before u send me ur code. r u using an emulator or actual device. if emulator, than check that GPS is installed or not in the AVD. if GPS is also installed in the AVD than go to DDMS and send mock locations to ur emulator.
if u r following my articles , i hope u must be missing something. Now about getting the location of a point other than the current GPS location. i ll post a tutorial soon, showing the latitude and logitude of the point that is touched the map. hope that will solve all ur problems.
April 18, 2012 at 8:56 am
Android Development
yes i am using an emulator and i sent dummy latitude and longitude and they were printed on the emulator using toast as you told in your 2nd tutorial. i am about to try it on actual device as soon as possible.
Thanks your all tutorials are good and helpful for every one who is beginner in this field.
April 16, 2012 at 9:28 am
Mir
Dear Shakeel, check the link below to get the location of a point. other than the current location of the GPS.
April 18, 2012 at 8:58 am
Android Development
Thanks for your kindness and helping me. I will try to check this tutorial and also will implement it
April 29, 2012 at 5:35 pm
Gus
Mir,
Thank you for putting together an amazing set of tutorials. They run through the google maps API at just the right speed.
I do have a silly and hopefully simple question for you though. Where do I find the mylocation.png,satelliteview.png,normalview.png icons that you use in this tutorial (and the cross icon for tutorial 3 for that matter)?
April 30, 2012 at 6:38 am
Mir
Thanks bro for appreciating my efforts.
n i like ur question, kind of i forgot to mention where we can get the pngs. the simplest way wud be, search ur own “C” drive with *.png. u will get tons of png icons. the icons that i have used are somewhere in the sdks that i downloaded. simple search for the pngs or check the sample projects in each sdk. u will get the icons of ur interest. if still u cant find than simply go to http://www.google.com/images and search for “x.png” u will get billions of crosses to choose from. same process for all other pngs.
hope this helps.
May 8, 2012 at 10:45 pm
G
Mir,
Many thanks. I wasn’t sure whether I was supposed to know where they were hidden away. You just found some suitable icons instead. Thanks for the reply, which was a lot quicker than mine. I will carry on working through your great tutorials.
Thanks
May 9, 2012 at 3:56 am
Mir
Reblogged this on Mir.
May 17, 2012 at 9:33 pm
AmateurAndyDeveloper
Hello Mir,
thanks a lot for the post, it is very much what I am trying to do. After tried executing this tutorial, i get a RunTimeException (Unable to instantiate activity ComponentInfo and ClassNotFoundException. Can u help me please (the stack trace is here http://pastebin.com/AuJmuPi2).
Thanks in advance.
May 18, 2012 at 4:57 am
Mir
Unable to instantiate activity ComponentInfo and ClassNotFound are two separate errors. and their can be alot of reasons that can cause them. The above code is fine as i have tested that on multiple machines. i ll suggest a few solutions and u can try them.
Check the sdk versions that i am using and that u r using and some other environment settings as well.
Check if ur activity is added in the AndroidManifest.xml
Check if the googlemaps library is added in the manifest file.
Check if u have the setContentView() defined in the onCreate method of ur main activity.
ClassNotFound can be due to a missing library. u need to check all ur libraries.
Hope that helps.
May 19, 2012 at 1:49 pm
AmateurAndyDeveloper
Hi Mir,
thanks for your suggestions…
we both are using the same sdk versions (version 10). Activity and permissions are added in the AndroidManifest.xml file. In the onCreate method, there is a setContentView is called with the parameter R.layout.main and main.xml file exists in the layout directory.
Things seems to be fine unless i have to add some other libraries. Other apps and maps apps too are working fine, so i think the basic libraries are existing, should I have to add any external libraries?(Is it possible that i missed something in your tutorial?)
Thanks again.
May 20, 2012 at 7:10 am
AmateurAndyDeveloper
Hi Mir,
i found that i had missed the google maps library in the manifest file. Thanks for your suggestions. I will work on the tutorial again n get back soon.
May 20, 2012 at 8:00 am
AmateurAndyDeveloper
Hi Mir. Thank you for your support, now i have the application running error free but i cannot see the buttons. Can you please post the manifest file. Are we not supposed to add anything into the manifest file (nothing beyond what we already have from the prev 3 tutorials)?
I am not able to see the menu…no options shown. But when the location is changed, I can see the Toast display and the cross marker which we add in the prev tutorial. Thank you very much.
PS: your menu.xml file has been distorted because of copy paste perhaps, please rectify that too.
June 1, 2012 at 10:57 am
sabu
haii mir
i appreciate your mind for giving these tutorials to all…
i am also a beginner in android i have an idea…how we can search the friends nearest me in these maps.. i think doing these a server is required..if yes can u explain how this possible in my project ..
plse help me…………………………..
June 1, 2012 at 3:41 pm
Mir
thanks for appreciating my efforts.
about locating nearest friends. u need to search by SIM number, Subscriber Identity module or mobile number of ur friends. u can not pin point them exactly but i think u can get the location of the tower to which ur friends are connected. what u can do it get a list of mobile numbers from ur contact list. than u might search each number to which tower a specific number is connected. and depending on the location of the tower to which u r connected u can calculate which friend is near u. this is just a wild idea. i havn’t tried it yet.
June 6, 2012 at 8:34 pm
Rick Rose
Thank you for your tutorial. Would you mind posting the xml file that contains the hidden TextView elements tvlat and tvlng? I get a null pointer error on my lines
tvlat.setText(“39.0”);
tvlng.setText(“-105.0”);
which attempts to set a test value.
Here’s the code for the MyLocationListener class and the gpsCurrentLocation class:
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
tvlat.setText(“39.0”);
tvlng.setText(“-105.0”);
this.gpsCurrentLocation();
}//end onLocationChanged method
@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)
{
// TODO Auto-generated method stub
}
public void gpsCurrentLocation()
{
String coordinates[] = {“”+tvlat.getText(), “”+tvlng.getText()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int)(lat * 1E6),
(int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(9);
MyMapOverlays marker = new MyMapOverlays(p);
List listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(marker);
mapView.invalidate();
}//end gpsCurrentLocation method
}//end MyLocationListener class
Here’s my xml:
Thanks!
Rick R.
June 7, 2012 at 4:27 am
Mir
wp comments dont allow xml code to be posted. as u can see, ur xml is also deleted. secondly i have changed machine an i dont have the code for that specific tutorial. but their is nothing technical in that xml file. just add two textviews on top of ur map and make their visibility false. assign them values as u display the lat,long in a toast and get the values from it and move the map to the desired location
July 12, 2012 at 10:35 am
Tohure
Dear Mir Nauman Tahir, I’m new and still inexperienced in programming android, and was checking your tutorials and there’s something I do not understand this, when you describe the menu.xml file labels as if they were poorly positioned, disordered or some left over. I hope you can help me understand better that file, because I may be looking at something this bad, thanks for your support.
July 12, 2012 at 3:34 pm
Mir
dear tohure
i have fixed the xml file. now u can try it.
July 31, 2012 at 9:06 am
Kenny
that’s so great, Mir. But I have a problem with GPS current location. I code the same, but i just only connect to onLocationChanged when i use Emulator control by mocking. It doesn’t get current location automatically when I click menu “My location”, even thought I put code “gpscurrentLocation()” in OnCreate(), it still doesn’t work…
Please help me if you know this trouble. Thanks for your support
August 2, 2012 at 3:37 am
Mir
dear kenny can u post the complete code of ur main activity file. also clearly state wat error do u encounter. or u just click the menu button and nothing happens
August 1, 2012 at 4:50 pm
Duy
Dear Mir
tvlat = (TextView)findViewById(R.id.tv_lat);
Erorr >> tv_lat cannot be resolved or is not a field
tvlong = (TextView)findViewById(R.id.tv_long);
Erorr >> tv_lat cannot be resolved or is not a field
I have create the ID’s for “tv_lat” and “tv_long”. I did this, but then I always get error messages.
thanks for your support !
August 1, 2012 at 8:01 pm
Duy
Greet I update my code
Very good tutorial. Thank you!
October 20, 2012 at 4:23 am
ney
how are your update duy, i meet same problem
August 18, 2012 at 3:51 pm
fujiakito
I wonder where should i create the id of R.id.tv_lat and R.id.tv_long that used in tvlat = (TextView)findViewById(R.id.tv_lat); and tvlong = (TextView)findViewById(R.id.tv_long); because i face the problem that it cannot be resolved or is not a field
August 27, 2012 at 6:13 am
Mir
you have to create them in the MapActivity Class. your main class. secondly create them in the global section, so that they are visible to all the methods. for reference check public class GooglemapsActivity extends MapActivity {
private TextView tvlat;
private TextView tvlong;
August 30, 2012 at 4:04 pm
fujiakito
i had done that part, but the R.id.tv_lat and R.id.tv_long cannot be resolved or is not a field.
August 20, 2012 at 1:39 pm
nixgoel
Hello Mr Mir
your tutorial is very good and helpful or many users. sir i am using your coding but i am unable to see my current location and even toast also on button click. if i give some latitude and longitude via ddms then i get that. Why i m not getting my current location. please give me some suggestion. please….
August 27, 2012 at 6:19 am
Mir
if u r using emulator than u need to set ur current location first by providing lat,long values. by default its set to some default location, now there are two ways to animate the map, either provide lat long from ddms, if its changed from the current map location than the map will animate to the current location. else nothing will happen. the other method is that u set ur current location from code. by hardcoding ur lat long values in ur code and than initiating the gpsCurrentLocation method. for more clarification check the following tutorial.
August 30, 2012 at 6:04 pm
nix
thank you sir for helping me and your suggestion. I am getting your point.
So i have to use real device if i want to find my exact location on different time for real application.
September 15, 2012 at 10:01 am
Ahmed
Very Nice Tutorial but Mir I am facing one problem, mapView.setBuiltInZoomControls(true); is not working if I dont use mapView.setClickable(true);. And if you later then onTouch doesn’t work. Please Help me out here. Thanks.
September 15, 2012 at 10:25 am
Why Zoom is not working with Google Maps? | Jisku.com - Developers Network
[…] Zoom is not working with Google Maps? I am following this tutorial enter link description here. The Problem is that mapView.setBuiltInZoomControls(true) doesn’t work at all…but when […]
September 25, 2012 at 3:44 am
Mir
if the zoom is not working that way, u can remove that line that automatically enables zooming controls. put a zoom control from the tools in design mode, and initiate the zooming functionality in its on click event
September 15, 2012 at 12:57 pm
Why Zoom is not working with Google Maps? | Free Android Enthusiasts
[…] am following this tutorial enter link description here. The Problem is that mapView.setBuiltInZoomControls(true) doesn’t work at all…but when […]
September 25, 2012 at 1:11 am
Wedar
can you send main.xml file to busananiuchi@gmail.com please. i got error to force close my aplication. sorry if my english is bad thanks
September 29, 2012 at 8:50 am
niki
ur post is great! thanx!
November 2, 2012 at 9:50 pm
Evan
Hi Mir,
Everything seems to work fine, but I’m having issues with the current location button where it sends me to another location instead of my current location once the button is pressed. Can you please advise? I have used your example codes above.
Thanks,
Evan
November 5, 2012 at 10:08 pm
Evan
Also, can you send me an example of your xml file?
November 6, 2012 at 10:30 am
Mir
first of all, are u using an emulator or testing it on actual device. if on emulator than check the coordinates in DDMS. when ever u click my current location it will take u to ur current location, if emulator than it will take u to the coordinates set in the DDMS. Find ur current location through google earth or google maps on internet. than put those coordinates in DDMS, than click the my current location n it will take you to ur current location. on device it will take you to ur current actual position.
November 6, 2012 at 7:56 pm
Evan
I used my actual device to test on. I also used both the device and emulator and both animates to the same position
November 8, 2012 at 9:27 pm
Evan
I believe it moves me to coordinates 0,0.
Please advise.
November 12, 2012 at 5:29 am
Mir
in GooglemapsActivity the tvLat, tvLong are initialized to zero. check ur code if ur changing that zero in onLocationcationChanged etc or any other funtion.
November 13, 2012 at 11:42 pm
Evan
Nothing is changed as it is the same as your code on top.
When I tilt the device, it does move to current location as the button my_location does not move me to current location.
Which part of the code should I look closely into in this case?
November 17, 2012 at 8:47 pm
Phil
Hello from France (sorry for my english)
Very good tuto
but when i m use “My Location” button,
i loose the zoom an return initial zoom
have you solution
great thanks
December 1, 2012 at 2:16 pm
Ifiok
Hello, I’m from Nigeria
When i select My Location, the marker animates to position but instead of seeing the map, i see blue screen. If i select satellite view, i see black screen. I get the same result when i test it on a real device. Please help
December 3, 2012 at 6:31 pm
Mir
this issue can be due to a wrong google maps api key. plz follow the tutorial to fix this probelm
December 3, 2012 at 6:33 pm
Mir
also check this tutorial update # 2
last part of this tutorial.
December 7, 2012 at 8:22 pm
Surge
Thanks for putting up this tutorial I really find it helpful, but I am running into a problem with the menu.xml file and getting an error ” Attribute is missing the Android namespace prefix” <item id="@+id/my_location" if you can help me out.
December 11, 2012 at 2:55 pm
Surge
Thanks for the awesome tutorial it helps a lot, but could be willing to post the menu.xml file, because I followed your tut but I keep running into errors like id=”@+id/my_location” it cant find : Attribute is missing the Android namespace prefix plus I cant find it in the graphical layout either
Please help any help is appreciated . Thanks again Mir this is awesome tutorial dude.
February 4, 2013 at 12:51 pm
Usman
instead of making hidden textviews, it is better to declare variable loc1 of type Location and pass the value of loc to loc1 and then jump to gpscurrentlocation.
February 28, 2013 at 7:44 pm
Maimoonah
Thanks.
I want to show 2 menus ,and O followed steps as your project without location showing..But the problem that the pictures didn’t appear when I ran the project…
This is my code:
Please If you can answer …
package com.example.androidtest2;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
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;
import com.google.android.maps.Overlay;
public class Map extends MapActivity{
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;
private TextView text1;
private TextView text2;
//On creat function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
//GPS
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0,0,locationListener);
//lat&log
text1 = (TextView) findViewById(R.id.longitude);
text2 = (TextView) findViewById(R.id.latitude);
//show map
mapView = (MapView) findViewById(R.id.map);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
// enable Street view by default
// mapView.setStreetView(true);
// enable to show Satellite view
// mapView.setSatellite(true);
// enable to show Traffic on map
// mapView.setTraffic(true);
//zoom
mapController.setZoom(16);
}
/////////////////////////add options
//Initiating Menu XML file (menu.xml)
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it’s id
* */
@SuppressWarnings(“deprecation”)
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.mapview_normal:
Toast.makeText(Map.this, “Map Normal Street View”, Toast.LENGTH_SHORT).show();
if(mapView.isSatellite()==true){
mapView.setSatellite(false);
}
return true;
case R.id.mapview_satelliteview:
Toast.makeText(Map.this, “Map Satallite View”, Toast.LENGTH_SHORT).show();
if(mapView.isSatellite()==false){
mapView.setSatellite(true);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected boolean isRouteDisplayed() {
return false;
}
//listener function to detect my location
public class GPSLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
if (location != null)
{
//gps point
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
// to show point
text1.setText(“Longitude now: “+String.valueOf((float)point.getLongitudeE6()/1000000));
text2.setText(“Latitude now: “+String.valueOf((float)point.getLatitudeE6()/1000000));
/* Toast.makeText(getBaseContext(),
“Latitude: ” + location.getLatitude() +
” Longitude: ” + location.getLongitude(),
Toast.LENGTH_SHORT).show();*/
mapController.animateTo(point);
mapController.setZoom(16);
// add marker
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
List listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
// String address = ConvertPointToLocation(point);
// Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
/* public String ConvertPointToLocation(GeoPoint point) {
String address = “”;
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
} */
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
class MapOverlay extends Overlay
{
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// convert point to pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y – 24, null); // 24 is the height of image
return true;
}
}
}
//_________________________________
/////////////////////////////////finish java
and the XML file is:
February 28, 2013 at 7:45 pm
Maimoonah
February 28, 2013 at 7:48 pm
Maimoonah
xmlns:android=”http://schemas.android.com/apk/res/android”
item android:id =”@+id/mapview_normal”
android:icon=”@drawable/normalview”
android:title=”Normal View”
item android:id =”@+id/mapview_satelliteview”
android:icon=”@drawable/satelliteview”
android:title=”Stalite View”
March 9, 2014 at 6:30 am
Ashish
Hi,Do u know how to add search functionality in google map.