Displaying Current Location on Google Maps
Posted By : Ravi Sharma | 12-Jul-2013
Once google maps api is successfully installed on your device you can enhance the api functionality by displaying your current location on the map.
You may refer the blog http://www.oodlestechnologies.com/blogs/Working-with-Google-Maps-in-Android in case you haven't used google map api before.
Here is the xml file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" /> </RelativeLayout>
In xml we have used the fragment view to display the maps.
And the code to show the location is what will do the remaining task.
package com.example.maptest; import java.io.IOException; import java.util.List; import java.util.Locale; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.MarkerOptions; 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.app.Activity; import android.content.Context; import android.widget.Toast; public class MainActivity extends Activity { Location currentLocation; Double currentLatitude; Double currentLongitude; String locationToBeSend; LocationManager locationManager; LocationListener locationListener; LatLng markOnMap; private GoogleMap map; String location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); getAddress(); } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 10000, 10, locationListener); } void getAddress() { try { Geocoder gcd = new Geocoder(MainActivity.this, Locale.getDefault()); List<Address> addresses = gcd.getFromLocation(currentLatitude, currentLongitude, 100); StringBuilder result = new StringBuilder(); if (addresses.size() > 0) { Address address = addresses.get(1); int maxIndex = address.getMaxAddressLineIndex(); for (int x = 0; x <= maxIndex; x++) { result.append(address.getAddressLine(x)); result.append(","); } } location = result.toString(); showLocationOnMap(currentLatitude, currentLongitude, location); } catch (IOException ex) { Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show(); } } void updateLocation(Location location) { currentLocation = location; currentLatitude = currentLocation.getLatitude(); currentLongitude = currentLocation.getLongitude(); } void showLocationOnMap(double latitude, double longitude, String location) { markOnMap = new LatLng(latitude, longitude); Marker mark = map .addMarker(new MarkerOptions() .position(markOnMap) .title(location) .snippet("You are here") .icon(BitmapDescriptorFactory .fromResource(R.drawable.map_icon))); map.moveCamera(CameraUpdateFactory.newLatLngZoom(markOnMap, 15)); map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); } @Override public void onDestroy() { try { locationManager.removeUpdates(locationListener); super.onDestroy(); } catch (Exception e) { } } }
We will be using the location listener to provide us the latitude, longitude for current location.
But to extract the current location we will be required to furthur use the Geocoder class.
The getFromLocation() method is what does the actual job.
It returns a list of addresses we are interested only in value at index 1.
The rest of the values provide only an outlay of current location but not exact location.
When you destroy the activity dont forget to remove updates, locationManager.removeUpdates(locationListener); does exactly that.
Also add internet, location permissions to manifest file.
Make sure device Gps is on.
This is what the outcome will look like on a real device.
Thanks,
Ravi Sharma
[email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ravi Sharma
Ravi Sharma is an Android application developer with experience in Java , Titanium and Phonegap frameworks. Ravi loves drawing and PC games.