You are currently browsing the tag archive for the ‘Google Maps Street View’ tag.
Tag Archive
Android Google Maps Tutorial Part 4, Adding Menu & Some Additional Functionality Like Zooming, Changing Map View, Animating To GPS Current Location Using Menu Button.
February 14, 2012 in Android Google Maps API Ver 1.0 | Tags: Android Google Maps API Ver 1.0, Animating Google Maps to Current Location, Google Map Satellite View, Google Maps, Google Maps Street View, GooglemapsActivity Class, gps location, mapview, Menu Creation, menu xml, My Location, using gps | 68 comments
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.

Normal View Button Clicked and if the map is not is normal view it will be converted to normal view.
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)