A Map shows my current location & a TextView shows my location name from geological coordinates (latitude& longitude) via Reverse Geo-coding.
App Preview |
Before you start: you need
- Google play service library in your Eclipse & Import it.
- Need API Key for Android applications from here.
1. Create a New Android Project called MapTest.
2. Open your MainActivity & paste this code. I added comments here necessary.
MainActivity
public class MainActivity extends Activity {
GoogleMap map;
TextView textView_Address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this a test comment about git
textView_Address = (TextView) findViewById(R.id.textView_Address);
setMapIfNeeded();
// getMyLocationAddress();
}
// set the map
private void setMapIfNeeded() {
if (map == null) {
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (map != null) {
setUpMap();
}
}
}
private void setUpMap() {
// set a icon on map to go your current position
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// get lat , long from best provider
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
// create an object with above two
LatLng current = new LatLng(latitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 13));
// set map zoom level
map.animateCamera(CameraUpdateFactory.zoomTo(13));
// add a maker to your position.
map.addMarker(new MarkerOptions().title("Your are here")
.snippet("The most populous city in Australia.")
.position(current));
getMyLocationAddress(latitude, longitude);
}
// Reverse Geocoding: get address name from latitude & longitude.
public String getMyLocationAddress(double lat, double longi) {
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
String address = "";
try {
// Place your latitude and longitude
List<Address> addresses = geocoder.getFromLocation(lat, longi, 1);
if (addresses != null) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for (int i = 0; i < fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append(
"\n");
}
textView_Address.setText("I am at: " + strAddress.toString());
address = strAddress.toString();
}
else
textView_Address.setText("No location found..!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Could not get address..!",
Toast.LENGTH_LONG).show();
}
return address;
}
}
activity_main.xml
<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" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
<TextView
android:id="@+id/textView_HeadLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="27dp"
android:text="My Location" />
<TextView
android:id="@+id/textView_Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_HeadLine"
android:layout_below="@+id/textView_HeadLine"
android:layout_marginTop="35dp"
android:text="Address" />
</RelativeLayout>
Update your manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.virtualcompiler.mapandlocationtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="insert your api key" />
<activity android:name="com.virtualcompiler.mapandlocationtest.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Don't forget Lazy Man is Now very very Busy.
Full SOurce Code: click me
Special Thanks to @kaushik @Dory @avijit