This video shows the steps to to get the location details such address, area, locality, city, state and country details from the latitude and longitude information. To get the information it uses Geocoder’s API getFromLocation.
It uses the App created in one of the previous videos as a starting point. Previous video can be watched using the below link. It is strongly recommended to watch this video before referring to the current video:
https://youtu.be/FknRti6n_F8
The previous App’s layout is modified by adding a button and Text Boxes to get the Latitude and Longitude information from the user and display the result in the text view.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Complete Source Code:
package com.example.myyoutubelocationapp;
import android.Manifest;
import android.content.pm.PackageManager;
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.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;import java.io.IOException;
import java.util.List;
import java.util.Locale;public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager locationManager;
private LocationListener locationListener;private final long MIN_TIME = 1000;
private final long MIN_DIST = 5;private LatLng latLng;
private TextView textView;
private EditText editTextLat;
private EditText editTextLong;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);textView = findViewById(R.id.textView);
editTextLat = findViewById(R.id.editTextLat);
editTextLong = findViewById(R.id.editTextong);latLng = new LatLng(-34, 151);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title(“Marker in Sydney”));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
try {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(“My Position”));mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
String phoneNumber = “99999”;
String myLatidude = String.valueOf(location.getAltitude());
String myLongitude = String.valueOf(location.getLongitude());String message = “Latitude = ” + myLatidude + ” Longitude = ” + myLongitude;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber,null,message,null,null);
}
catch (Exception e){
e.printStackTrace();
}
}@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}@Override
public void onProviderEnabled(String provider) {
}@Override
public void onProviderDisabled(String provider) {
}
};locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME,MIN_DIST,locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME,MIN_DIST,locationListener);
}
catch (SecurityException e){
e.printStackTrace();
}
}public void getLocationDetails(View view){
double latitude = latLng.latitude;
double longitude = latLng.longitude;
if (!(editTextLong.getText().toString().isEmpty() || editTextLat.getText().toString().isEmpty()))
{
latitude = Double.parseDouble(editTextLat.getText().toString());
longitude = Double.parseDouble(editTextLong.getText().toString());
latLng = new LatLng(latitude, longitude);
}
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());String address = null;
String city = null;
String state = null;
String country = null;
String postalCode = null;
String knonName = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knonName = addresses.get(0).getFeatureName();
} catch (IOException e) {
e.printStackTrace();
}
mMap.addMarker(new MarkerOptions().position(latLng).title(“Marker in : ” + address + city + state + country + postalCode + knonName));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
textView.setText(address + city + state + country + postalCode + knonName);
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myyoutubelocationapp”><!–
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the ‘MyLocation’ functionality.
–>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.SEND_SMS”/><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”><!–
The API key for Google Maps-based APIs is defined as a string resource.
(See the file “res/values/google_maps_api.xml”).
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
–>
<meta-data
android:name=”com.google.android.geo.API_KEY”
android:value=”@string/google_maps_key” /><activity
android:name=”.MapsActivity”
android:label=”@string/title_activity_maps”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout 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=”.MapsActivity”><fragment xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/map”
android:name=”com.google.android.gms.maps.SupportMapFragment”
android:layout_width=”match_parent”
android:layout_height=”550dp”
tools:context=”.MapsActivity” /><Button
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”300dp”
android:layout_marginTop=”600dp”
android:text=”@string/GetLocation”
android:onClick=”getLocationDetails”
/><TextView
android:id=”@+id/textView”
android:layout_width=”280dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”50dp”
android:layout_marginTop=”550dp”
android:text=”@string/LocationDisplay”
/><EditText
android:id=”@+id/editTextLat”
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”300dp”
android:layout_marginTop=”500dp”
android:hint=”@string/Latitude”
/><EditText
android:id=”@+id/editTextong”
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”300dp”
android:layout_marginTop=”550dp”
android:hint=”@string/Longitude”
/></FrameLayout>
uyyyyufufcujck
???