Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active July 27, 2021 11:10
Show Gist options
  • Save enginebai/adcae1f17d3b2114590c to your computer and use it in GitHub Desktop.
Save enginebai/adcae1f17d3b2114590c to your computer and use it in GitHub Desktop.
Get the current location and display a marker on Google Map.
package com.enginebai.sample;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.orhanobut.logger.Logger;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
public static final int LOCATION_UPDATE_MIN_DISTANCE = 10;
public static final int LOCATION_UPDATE_MIN_TIME = 5000;
@Bind(R.id.mapview)
MapView mMapView;
@Bind(R.id.toolbar)
Toolbar mToolbar;
@Bind(R.id.fab)
FloatingActionButton mFab;
private GoogleMap mGoogleMap;
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Logger.d(String.format("%f, %f", location.getLatitude(), location.getLongitude()));
drawMarker(location);
mLocationManager.removeUpdates(mLocationListener);
} else {
Logger.d("Location is null");
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private LocationManager mLocationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Logger.init();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getCurrentLocation();
}
});
mMapView.onCreate(savedInstanceState);
mGoogleMap = mMapView.getMap();
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
initMap();
getCurrentLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
getCurrentLocation();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
mLocationManager.removeUpdates(mLocationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initMap() {
int googlePlayStatus = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (googlePlayStatus != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(googlePlayStatus, this, -1).show();
finish();
} else {
if (mGoogleMap != null) {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setAllGesturesEnabled(true);
}
}
}
private void getCurrentLocation() {
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location = null;
if (!(isGPSEnabled || isNetworkEnabled))
Snackbar.make(mMapView, R.string.error_location_provider, Snackbar.LENGTH_INDEFINITE).show();
else {
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (isGPSEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
if (location != null) {
Logger.d(String.format("getCurrentLocation(%f, %f)", location.getLatitude(),
location.getLongitude()));
drawMarker(location);
}
}
private void drawMarker(Location location) {
if (mGoogleMap != null) {
mGoogleMap.clear();
LatLng gps = new LatLng(location.getLatitude(), location.getLongitude());
mGoogleMap.addMarker(new MarkerOptions()
.position(gps)
.title("Current Position"));
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(gps, 12));
}
}
}
@ELTEGANI
Copy link

ELTEGANI commented Oct 22, 2017

thanks

@fanshaohui
Copy link

thank you so mach

@saileshnarayanan
Copy link

Thanks ..but still getting an error for location.Can you please list down all the JAR files that you have used.Im trying to fetch current location but unable to do so.

@ekaperintis
Copy link

Thanks

@AbdulRehmanAB
Copy link

sir how ro add marshmallow runtime permisson in the google maps ?????????? plz update this with marshmallow thanks

@tianlongli124
Copy link

Thanks. But If can, post all project. regards

@SENYANGE
Copy link

thanks

@MoeidHeidari
Copy link

You can use this library
https://github.com/MoeidHeidari/LocationTracker
public class MainActivity extends AppCompatActivity {
LocationTracker tracker;
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tracker =new LocationTracker(this);
Toast.makeText(this, String.valueOf(tracker.getLatitude()),
Toast.LENGTH_LONG).show();
tracker.setOnLocationChanged(new OnLocationChanged() {
@OverRide
public void OnChange(Location location) {

            //Write your codes here
        }
    });


}

}

@dya4427
Copy link

dya4427 commented Nov 3, 2018

it doesnt work, it sais "cannot resolve simbol" on almost every word. what can i do

@attendances
Copy link

thank you so much

@Parv-Maheshwari
Copy link

Thank you so much, This worked like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment