This page shows the steps to create an App which uses Google Map to give the precise location of your phone. This app can be used to track your current position. In the initial part of the video it shows how to create API Key to have the access of the google Map.
This custom app works offline based on GPS and you do not need internet connection to get your current location.
Source code :
package com.example.mylocation.mylocation;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;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;public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationListener locationListener;
private LocationManager locationManager;private final long MIN_TIME = 1000; // 1 second
private final long MIN_DIST = 5; // 5 Metersprivate LatLng latLng;
@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.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);}
/**
* 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));
}
catch (SecurityException e){
e.printStackTrace();
}}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String s) {}
@Override
public void onProviderDisabled(String s) {}
};
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
}
catch (SecurityException e){
e.printStackTrace();
}
}
}
<?xml version=”1.0? encoding=”utf-8??>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mylocation.mylocation”>
<!–
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”/>
<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>
<fragment xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:map=”http://schemas.android.com/apk/res-auto”
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=”match_parent”
tools:context=”com.example.mylocation.mylocation.MapsActivity” />
Top posts/ comments from YouTube channel
How can we send the location of the person to the registered phone number without internet?
The only way (without internet) to share your location details would be through sms. Get the latitude and longitude information using GPS in the map and send it through SMS to other person. Of course for this one will need access to SMS service in the location app. This can be done by updating manifest file.
Thanks
Thank you so much!
hello sir the application is working but whenever it changes the location new pinpoint appear then the previous pinpoints will remain. How can i remove those previous pinpoints and only have the newest one
Easiest way to do this is by clearing the map with all the previous marker by using below line of code:
mMap.clear();
just add the above line of code before addMarker code as shown in below:
try {
mMap.clear();
latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(“My Position”));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
catch (SecurityException e){
e.printStackTrace();
}
Hope the above helps.
Cheers
Programmer World
–
hello sir the application is working but whenever it changes the location new pinpoint appear then the previous pinpoints will remain. how can i remove those previous pinpoints and only have the newest one. The other problem is, we tried this on 4 different phones, for the 2 phones it run smoothly it can locate the exact location no problem at all (just the pinpoint) but for the other 2 it cant locate its location. and for the last one, is it possible to draw an object in the map? for example a circle with the pin location in the center of the circle. Thank you!!!!!
Qs: how can i remove those previous pinpoints and only have the newest one.
Just add mMap.clear() to remove all the markers before adding the new marker.
Qs The other problem is, we tried this on 4 different phones, for the 2 phones it run smoothly it can locate the exact location no problem at all (just the pinpoint) but for the other 2 it cant locate its location. and for the last one
Just ensure that GPS and Data network is ON and location access permissions (Coarse and Fine) both are granted to the App in the phone. Also, sometimes in some phones there may be restriction on using certain permissions for the user Apps. Just check that by going to Settings->Apps->Permission. A quick fix for this will be to just reinstall the App after removing the App (including the App data).
Qs: is it possible to draw an object in the map? for example a circle with the pin location in the center of the circle.
There are lots of properties of markers which allows to customize the shape, color, size, etc. of the marker. As of now I do not have a video to show this but below page will help you with this:
https://developers.google.com/maps/documentation/android-sdk/marker
Hope above details helps.
Cheers
Programmer World
–