Advertisement
Guest User

Map Pedi

a guest
Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.34 KB | None | 0 0
  1. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, DirectionFinderListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
  2. {
  3. private Geocoder geocoder;
  4. private GoogleMap mMap;
  5. private FloatingActionButton btnFindPath;
  6. private TextView btnLanjutkan;
  7. private TextView btnLihatLokasi;
  8. private EditText etOrigin;
  9. private EditText etDestination;
  10. private List<Marker> originMarkers = new ArrayList<>();
  11. private List<Marker> destinationMarkers = new ArrayList<>();
  12. private List<Polyline> polylinePaths = new ArrayList<>();
  13. private List<Address> addresses;
  14. private ProgressDialog progressDialog;
  15. protected LocationManager locationManager;
  16. private Location mLastLocation;
  17. private Context context;
  18. protected String latitude, longitude;
  19. private FusedLocationProviderClient mFusedLocationClient;
  20. protected double lng, lat;
  21. private GoogleApiClient googleApiClient;
  22. private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
  23. private int pid;
  24. TextView ongkir;
  25. private int market = 0;
  26. SupportMapFragment mapFragment;
  27. private boolean isMarket;
  28. private Marker dest;
  29. private boolean fromsearch = false;
  30. private ImageView BBack;
  31.  
  32. Dialog dialog;
  33.  
  34. private void AskMarket()
  35. {
  36. dialog = new Dialog(MapsActivity.this);
  37. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  38. dialog.setCancelable(false);
  39. dialog.setContentView(R.layout.dialog_three_button);
  40.  
  41. TextView BOne = dialog.findViewById(R.id.BOne);
  42. TextView BTwo = dialog.findViewById(R.id.BTwo);
  43. TextView BThree = dialog.findViewById(R.id.BThree);
  44. TextView LDesc = dialog.findViewById(R.id.LDesc);
  45. TextView title = dialog.findViewById(R.id.title);
  46. title.setText("Pilih Market");
  47. LDesc.setText("Untuk melanjutkan, silakan pilih Market yang dikehendaki");
  48.  
  49. BOne.setText("Pasar Manis");
  50. BTwo.setText("Pasar Wage");
  51. BThree.setText("Pasar Cermei");
  52.  
  53. BOne.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. market = 0;
  57. InitGMaps();
  58. }
  59. });
  60.  
  61. BTwo.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. market = 1;
  65. InitGMaps();
  66. }
  67. });
  68.  
  69. BThree.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View v) {
  72. market = 2;
  73. InitGMaps();
  74. }
  75. });
  76.  
  77. dialog.show();
  78. }
  79.  
  80. @Override
  81. protected void onCreate(Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. setContentView(R.layout.activity_maps);
  84.  
  85. isMarket = getIntent().getBooleanExtra("isMarket", true);
  86.  
  87. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  88. if (isMarket)
  89. AskMarket();
  90. else
  91. {
  92. mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  93. mapFragment.getMapAsync(this);
  94. }
  95.  
  96. ongkir = (TextView) findViewById(R.id.tvPrice);
  97. btnLihatLokasi = (TextView) findViewById(R.id.btnLihatLokasi);
  98. btnFindPath = (FloatingActionButton) findViewById(R.id.btnFindPath);
  99. btnLanjutkan = (TextView) findViewById(R.id.btnLanjutkan);
  100. etOrigin = (EditText) findViewById(R.id.etOrigin);
  101. etDestination = (EditText) findViewById(R.id.etDestination);
  102. BBack = findViewById(R.id.BBack);
  103.  
  104. BBack.setOnClickListener(new View.OnClickListener()
  105. {
  106. @Override
  107. public void onClick(View v)
  108. {
  109. onBackPressed();
  110. }
  111. });
  112.  
  113. if (checkPlayServices()) {
  114. // Building the GoogleApi client
  115. buildGoogleApiClient();
  116. }
  117.  
  118. btnLihatLokasi.setOnClickListener(new View.OnClickListener() {
  119. @Override
  120. public void onClick(View view) {
  121. btnLihatLokasi.setEnabled(false);
  122. requestmyloc();
  123. callatruntime();
  124. }
  125. });
  126.  
  127. btnFindPath.setOnClickListener(new View.OnClickListener() {
  128. @Override
  129. public void onClick(View v) {
  130. fromsearch = true;
  131. sendRequest(etDestination.getText().toString());
  132. }
  133. });
  134.  
  135. btnLanjutkan.setOnClickListener(new View.OnClickListener() {
  136. @Override
  137. public void onClick(View view) {
  138. if (etDestination.getText().toString().isEmpty()) {
  139. Toast.makeText(getApplicationContext(), "Isi Alamat Terlbih Dahulu", Toast.LENGTH_SHORT).show();
  140. } else if (ongkir.getText().toString().equals("0")) {
  141. Toast.makeText(getApplicationContext(), "Klik Tombol Pencarian untuk Mengetahui ongkir", Toast.LENGTH_SHORT).show();
  142. } else {
  143. String tujuan = etDestination.getText().toString();
  144. int ongkos = Integer.valueOf(ongkir.getText().toString());
  145. Intent intent = new Intent(getApplicationContext(), FinishMarketActivity.class);
  146. intent.putExtra("isMarket", isMarket);
  147. intent.putExtra("tujuan", tujuan);
  148. intent.putExtra("ongkir", ongkos);
  149. intent.putExtra("market", market);
  150. startActivity(intent);
  151. }
  152.  
  153. }
  154. });
  155. }
  156.  
  157. public void callatruntime() {
  158. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  159. checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  160. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, pid);
  161. } else {
  162. Toast.makeText(this, "Location Permission Granted", Toast.LENGTH_SHORT).show();
  163.  
  164. }
  165. }
  166.  
  167. @Override
  168. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  169. if (requestCode == pid) {
  170. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  171. callatruntime();
  172. } else {
  173. Toast.makeText(getApplicationContext(), "Not Granted", Toast.LENGTH_LONG).show();
  174. }
  175.  
  176. }
  177. }
  178.  
  179. private void requestmyloc() {
  180.  
  181. btnLihatLokasi.setEnabled(true);
  182. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  183. // TODO: Consider calling
  184. // ActivityCompat#requestPermissions
  185. // here to request the missing permissions, and then overriding
  186. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  187. // int[] grantResults)
  188. // to handle the case where the user grants the permission. See the documentation
  189. // for ActivityCompat#requestPermissions for more details.
  190. return;
  191. }
  192. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
  193. if (mLastLocation != null) {
  194. double latitude = mLastLocation.getLatitude();
  195. double longitude = mLastLocation.getLongitude();
  196. String origin = etOrigin.getText().toString() + " Purwokerto";
  197.  
  198. Log.d("Longitude", String.valueOf(longitude));
  199. etDestination.setText(GetAddress(latitude, longitude));
  200. sendRequest(String.valueOf(latitude + "," + longitude));
  201.  
  202. } else {
  203. Log.d("error", "message");
  204. }
  205. }
  206.  
  207. private String GetAddress(double lat, double lng)
  208. {
  209. Geocoder geocoder = new Geocoder(this);
  210. try
  211. {
  212. List<Address> addresses = geocoder.getFromLocation(lat, lng,1);
  213. String address = addresses.get(0).getAddressLine(0);
  214. String city = addresses.get(0).getAddressLine(1);
  215. //String country = addresses.get(0).getAddressLine(2);
  216.  
  217. if (address != null){
  218. return address;
  219. }
  220.  
  221. } catch (IOException e) {
  222. e.printStackTrace();
  223. }
  224. return "Tidak Ditemukan";
  225. }
  226.  
  227. private void sendRequest(String destination) {
  228.  
  229. String origin = etOrigin.getText().toString() + " Purwokerto";
  230. if (origin.isEmpty()) {
  231. Toast.makeText(this, "Please enter origin address!", Toast.LENGTH_SHORT).show();
  232. return;
  233. }
  234. if (destination.isEmpty()) {
  235. Toast.makeText(this, "Please enter destination address!", Toast.LENGTH_SHORT).show();
  236. return;
  237. }
  238.  
  239. Log.d("##############", "sendRequest: " + destination);
  240.  
  241. try {
  242. new DirectionFinder(this, origin, destination).execute();
  243. } catch (UnsupportedEncodingException e) {
  244. e.printStackTrace();
  245. }
  246.  
  247. }
  248.  
  249. private void InitGMaps()
  250. {
  251. dialog.cancel();
  252. mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  253. mapFragment.getMapAsync(this);
  254. }
  255.  
  256. @Override
  257. public void onMapReady(GoogleMap googleMap) {
  258. mMap = googleMap;
  259.  
  260. mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
  261. {
  262. @Override
  263. public void onMapClick(LatLng latLng)
  264. {
  265. fromsearch = false;
  266. dest.setVisible(true);
  267. dest.setPosition(latLng);
  268. sendRequest(String.valueOf(latLng.latitude + "," + latLng.longitude));
  269. etDestination.setText(GetAddress(latLng.latitude, latLng.longitude));
  270. }
  271. });
  272.  
  273. LatLng hcmus = new LatLng(-7.417905, 109.227261);
  274.  
  275. dest = mMap.addMarker(new MarkerOptions().title("Tujuan").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_blue2)).position(hcmus));
  276. dest.setVisible(false);
  277.  
  278. String marketname = "Pasar Manis";
  279. if (isMarket)
  280. {
  281. switch (market)
  282. {
  283. case 0:
  284. {
  285. hcmus = new LatLng(-7.417905, 109.227261);
  286. marketname = "Pasar Manis";
  287. break;
  288. }
  289. case 1:
  290. {
  291. hcmus = new LatLng(-7.426289, 109.250309);
  292. marketname = "Pasar Wage";
  293. break;
  294. }
  295. case 2:
  296. {
  297. hcmus = new LatLng(-7.397284, 109.232166);
  298. marketname = "Pasar Cerme";
  299. break;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. marketname = "Kopkun Swalayan";
  306. hcmus = new LatLng(-7.407896999999999, 109.24528499999997);
  307. }
  308. etOrigin.setText(marketname);
  309. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(hcmus, 18));
  310. originMarkers.add(mMap.addMarker(new MarkerOptions()
  311. .title(marketname)
  312. .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location2))
  313. .position(hcmus))
  314. );
  315.  
  316. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  317. // TODO: Consider calling
  318. // ActivityCompat#requestPermissions
  319. // here to request the missing permissions, and then overriding
  320. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  321. // int[] grantResults)
  322. // to handle the case where the user grants the permission. See the documentation
  323. // for ActivityCompat#requestPermissions for more details.
  324. return;
  325. }
  326. mMap.setMyLocationEnabled(true);
  327. }
  328.  
  329.  
  330. @Override
  331. public void onDirectionFinderStart() {
  332. progressDialog = ProgressDialog.show(this, "Please wait.",
  333. "Finding direction..!", true);
  334.  
  335. if (originMarkers != null) {
  336. for (Marker marker : originMarkers) {
  337. marker.remove();
  338. }
  339. }
  340.  
  341. if (destinationMarkers != null) {
  342. for (Marker marker : destinationMarkers) {
  343. marker.remove();
  344. }
  345. }
  346.  
  347. if (polylinePaths != null) {
  348. for (Polyline polyline:polylinePaths ) {
  349. polyline.remove();
  350. }
  351. }
  352. }
  353.  
  354. @OnClick(R.id.BBack)
  355. public void back(){
  356. onBackPressed();
  357. }
  358.  
  359. @Override
  360. public void onDirectionFinderSuccess(List<Route> routes) {
  361. progressDialog.dismiss();
  362. polylinePaths = new ArrayList<>();
  363. originMarkers = new ArrayList<>();
  364. destinationMarkers = new ArrayList<>();
  365.  
  366. for (Route route : routes) {
  367. if (fromsearch)
  368. {
  369. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.endLocation, 18));
  370.  
  371. destinationMarkers.add(mMap.addMarker(new MarkerOptions()
  372. .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_blue2))
  373. .title(route.endAddress)
  374. .position(route.endLocation)));
  375. }
  376.  
  377. TextView jarak = (TextView) findViewById(R.id.tvDistance);
  378. TextView waktu = (TextView) findViewById(R.id.tvDuration);
  379. jarak.setText(String.valueOf(route.distance.value));
  380. waktu.setText(String.valueOf(route.duration.value));
  381.  
  382. btnLihatLokasi.setEnabled(false);
  383. int changeJarak = Integer.parseInt(jarak.getText().toString());
  384. int changeWaktu = Integer.parseInt(waktu.getText().toString());
  385. int awal = 7700;
  386. int jarakawal = changeJarak/1000;
  387.  
  388. if (jarakawal < 5){
  389. ongkir.setText(String.valueOf(awal));
  390. jarak.setText(String.valueOf(changeJarak/1000));
  391. waktu.setText(String.valueOf(changeWaktu/60));
  392. } else {
  393. ongkir.setText(String.valueOf((jarakawal - 5) * 1600 + awal));
  394. jarak.setText(String.valueOf(changeJarak/1000));
  395. waktu.setText(String.valueOf(changeWaktu/60));
  396. }
  397.  
  398. //((TextView) findViewById(R.id.tvPrice)).setText(route.price.text);
  399.  
  400. originMarkers.add(mMap.addMarker(new MarkerOptions()
  401. .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location2))
  402. .title(route.startAddress)
  403. .position(route.startLocation)));
  404.  
  405. PolylineOptions polylineOptions = new PolylineOptions().
  406. geodesic(true).
  407. color(R.color.colorPrimary).
  408. width(10);
  409.  
  410. for (int i = 0; i < route.points.size(); i++)
  411. polylineOptions.add(route.points.get(i));
  412.  
  413. polylinePaths.add(mMap.addPolyline(polylineOptions));
  414. }
  415. }
  416.  
  417. /**
  418. * Creating google api client object
  419. * */
  420. protected synchronized void buildGoogleApiClient() {
  421. googleApiClient = new GoogleApiClient.Builder(this)
  422. .addConnectionCallbacks(this)
  423. .addOnConnectionFailedListener(this)
  424. .addApi(LocationServices.API).build();
  425. }
  426.  
  427. /**
  428. * Method to verify google play services on the device
  429. * */
  430. private boolean checkPlayServices() {
  431. int resultCode = GooglePlayServicesUtil
  432. .isGooglePlayServicesAvailable(this);
  433. if (resultCode != ConnectionResult.SUCCESS) {
  434. if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  435. GooglePlayServicesUtil.getErrorDialog(resultCode, this,
  436. PLAY_SERVICES_RESOLUTION_REQUEST).show();
  437. } else {
  438. Toast.makeText(getApplicationContext(),
  439. "This device is not supported.", Toast.LENGTH_LONG)
  440. .show();
  441. finish();
  442. }
  443. return false;
  444. }
  445. return true;
  446. }
  447.  
  448. @Override
  449. protected void onStart() {
  450. super.onStart();
  451. if (googleApiClient != null) {
  452. googleApiClient.connect();
  453. }
  454. }
  455.  
  456. @Override
  457. protected void onResume() {
  458. super.onResume();
  459.  
  460. checkPlayServices();
  461. }
  462.  
  463. @Override
  464. public void onConnected(@Nullable Bundle bundle) {
  465. /*requestmyloc();*/
  466. }
  467.  
  468. @Override
  469. public void onConnectionSuspended(int i) {
  470. googleApiClient.connect();
  471. }
  472.  
  473. @Override
  474. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  475.  
  476. }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement