You are on page 1of 6

package ac.id.atmaluhur.

uastest;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import ac.id.atmaluhur.uastest.api.ApiClient;
import ac.id.atmaluhur.uastest.api.ApiService;
import ac.id.atmaluhur.uastest.model.ListLocationModel;
import ac.id.atmaluhur.uastest.model.LocationModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,


GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMarkerDragListener {

private GoogleMap mMap;


GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
Location mLastLocation;
Marker mCurrLocationMarker;
int PROXIMITY_RADIUS = 10000;
double latitude, longitude;
double end_latitude, end_longitude;

private List<LocationModel> mListMarker = new ArrayList<>();

@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.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}

if (!CheckGooglePlayServices()) {
Log.d("onCreate", "Finishing test case since google Play Services are
not available");
finish();
} else {
Log.d("onCreate", "Google Play services available.");
}
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(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;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
getAllDataLocationLatLng();
}

private boolean CheckGooglePlayServices() {


GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS) {
if (googleAPI.isUserResolvableError(result)) {
googleAPI.getErrorDialog(this, result, 0).show();
}
return false;
}
return true;
}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

public boolean checkLocationPermission() {


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
} else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}

@Override
public void onLocationChanged(Location location) {

Log.d("onLocationChanged", "entered");

mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}

latitude = location.getLatitude();
longitude = location.getLongitude();

LatLng latLng = new LatLng(location.getLatitude(),


location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
// markerOptions.draggable(true);
markerOptions.title("Current Position");

markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HU
E_MAGENTA));
mCurrLocationMarker = mMap.addMarker(markerOptions);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

Toast.makeText(MapsActivity.this, "Your Current Location",


Toast.LENGTH_LONG).show();
Log.d("onLocationChanged", String.format("latitude:%.3f longitude:
%.3f",latitude,longitude));
if (mGoogleApiClient != null) {

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Log.d("onLocationChanged", "Removing Location Updates");
}
}

public void onClick(View v) {


switch (v.getId()) {
case R.id.B_search:
EditText addressField = (EditText) findViewById(R.id.TF_location);
String address = addressField.getText().toString();

List<Address> addressList = null;


MarkerOptions userMarkerOptions = new MarkerOptions();

if (!TextUtils.isEmpty(address)) {
Geocoder geocoder = new Geocoder(this);

try {
addressList = geocoder.getFromLocationName(address, 6);

if (addressList != null) {
for (int i = 0; i < addressList.size(); i++) {
Address userAddress = addressList.get(i);
LatLng latLng = new
LatLng(userAddress.getLatitude(), userAddress.getLongitude());

userMarkerOptions.position(latLng);
userMarkerOptions.title(address);

userMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactor
y.HUE_ORANGE));
mMap.addMarker(userMarkerOptions);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}
} else {
Toast.makeText(this, "Location not found...",
Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "please write any location name...",
Toast.LENGTH_SHORT).show();
}
break;

}
}

private void getAllDataLocationLatLng() {


final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Menampilkan data marker ..");
dialog.show();
ApiService apiService = ApiClient.getClient().create(ApiService.class);
Call<ListLocationModel> call = apiService.getAllLocation();
call.enqueue(new Callback<ListLocationModel>() {
@Override
public void onResponse(Call<ListLocationModel> call,
Response<ListLocationModel> response) {
dialog.dismiss();
mListMarker = response.body().getmData();
initMarker(mListMarker);
}

@Override
public void onFailure(Call<ListLocationModel> call, Throwable t) {
dialog.dismiss();
Toast.makeText(MapsActivity.this, t.getMessage(),
Toast.LENGTH_SHORT).show();

private void initMarker(List<LocationModel> mListMarker) {


//iterasi semua data dan tampilkan markernya
for (int i = 0; i < mListMarker.size(); i++) {
//set latlng nya
LatLng location = new
LatLng(Double.parseDouble(mListMarker.get(i).getLatitude()),
Double.parseDouble(mListMarker.get(i).getLongitude()));
//tambahkan markernya
mMap.addMarker(new
MarkerOptions().position(location).title(mListMarker.get(i).getImageLocationName())
);
//set latlng index ke 0
LatLng latLng = new
LatLng(Double.parseDouble(mListMarker.get(0).getLatitude()),
Double.parseDouble(mListMarker.get(0).getLongitude()));
//lalu arahkan zooming ke marker index ke 0
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(latLng.latitude, latLng.longitude), 11.0f));
}
}
});
}

@Override
public void onConnected(@Nullable Bundle bundle) {

@Override
public void onConnectionSuspended(int i) {

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

@Override
public boolean onMarkerClick(Marker marker) {
return false;
}

@Override
public void onMarkerDragStart(Marker marker) {

@Override
public void onMarkerDrag(Marker marker) {

@Override
public void onMarkerDragEnd(Marker marker) {

}
}

You might also like