Skip to content

Instantly share code, notes, and snippets.

@ameliacv
Last active February 10, 2018 08:12
Show Gist options
  • Save ameliacv/481178320ca968a8f448707d10a02b9b to your computer and use it in GitHub Desktop.
Save ameliacv/481178320ca968a8f448707d10a02b9b to your computer and use it in GitHub Desktop.
public class MapsNearbyActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Member mMember;
private GPSTracker gps;
private double mLong, mLat;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_nearby);
ButterKnife.bind(this);
// 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);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
gps = new GPSTracker(this);
}
/**
* 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;
if(gps.canGetLocation()){
mLat = gps.getLatitude();
mLong = gps.getLongitude();
if(mLat != 0 && mLong != 0) {
LatLng latLng = new LatLng(mLat, mLong);
mMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
// \n is for new line
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
// Add a marker in Sydney and move the camera
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment