Advertisement
lala6666

problem

Sep 2nd, 2018
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.32 KB | None | 0 0
  1. package com.skripsi.axioo.percobaan5;
  2.  
  3. import android.Manifest;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.pm.PackageManager;
  9. import android.os.Bundle;
  10. import android.support.v4.app.ActivityCompat;
  11. import android.support.v4.app.FragmentActivity;
  12. import android.support.v7.app.AlertDialog;
  13. import android.util.Log;
  14. import android.view.Gravity;
  15. import android.view.View;
  16. import android.view.inputmethod.InputMethodManager;
  17. import android.widget.AutoCompleteTextView;
  18. import android.widget.EditText;
  19. import android.widget.ImageView;
  20. import android.widget.RelativeLayout;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23.  
  24. import com.android.volley.Request;
  25. import com.android.volley.Response;
  26. import com.android.volley.VolleyError;
  27. import com.android.volley.toolbox.JsonArrayRequest;
  28. import com.android.volley.toolbox.StringRequest;
  29. import com.android.volley.toolbox.Volley;
  30. import com.google.android.gms.common.api.Status;
  31. import com.google.android.gms.location.places.AutocompleteFilter;
  32. import com.google.android.gms.location.places.Place;
  33. import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
  34. import com.google.android.gms.location.places.ui.PlaceSelectionListener;
  35. import com.google.android.gms.maps.CameraUpdateFactory;
  36. import com.google.android.gms.maps.GoogleMap;
  37. import com.google.android.gms.maps.MapFragment;
  38. import com.google.android.gms.maps.OnMapReadyCallback;
  39. import com.google.android.gms.maps.SupportMapFragment;
  40. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  41. import com.google.android.gms.maps.model.CameraPosition;
  42. import com.google.android.gms.maps.model.LatLng;
  43. import com.google.android.gms.maps.model.Marker;
  44. import com.google.android.gms.maps.model.MarkerOptions;
  45. import com.skripsi.axioo.percobaan5.sugestipencarian.AppController;
  46. import com.skripsi.axioo.percobaan5.sugestipencarian.SuggestionAdapter;
  47.  
  48. import org.json.JSONArray;
  49. import org.json.JSONException;
  50. import org.json.JSONObject;
  51.  
  52. import java.io.UnsupportedEncodingException;
  53. import java.util.Arrays;
  54.  
  55. public class MapsActivity extends FragmentActivity implements
  56. OnMapReadyCallback, View.OnClickListener {
  57.  
  58. private GoogleMap mMap;
  59. PlaceAutocompleteFragment placeAutoComplete;
  60. SupportMapFragment mapFragment;
  61. CameraPosition cameraPosition;
  62.  
  63. String[] id, nama, gambar, alamat, harga, jam, komentar, informasi,fasilitas, telepon, distance;
  64. int numData;
  65. LatLng center, latLng[];
  66. GoogleMap gMap;
  67. Boolean markerD[];
  68. private Double[] latitude, longitude;
  69. String ambilcari;
  70. ProgressDialog dialog;
  71. String lokasi;
  72. ImageView cari;
  73. Double lati, longi;
  74. String tag_json_obj = "json_obj_req";
  75. AutoCompleteTextView acTextView;
  76. public static final String LAT = "lat";
  77. public static final String LNG = "lng";
  78. EditText pencarian;
  79.  
  80.  
  81. @Override
  82. protected void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.activity_maps);
  85. acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
  86. acTextView.setAdapter(new SuggestionAdapter(this, acTextView.getText().toString()));
  87. cari = (ImageView) findViewById(R.id.btncari);
  88.  
  89. cari.setOnClickListener(this);
  90. mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
  91. mapFragment.getMapAsync(this);
  92. dialog = new ProgressDialog(this);
  93. View locationButton = ((View) findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
  94. RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
  95. // position on right bottom
  96. rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
  97. rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
  98. rlp.setMargins(0, 0, 30, 30);
  99.  
  100. getLokasi();
  101.  
  102. }
  103.  
  104. private void getLokasi() {
  105. String url = "https://arizalway.000webhostapp.com/api/sekolah.php";
  106. JsonArrayRequest request = new JsonArrayRequest
  107. (Request.Method.GET, url, new Response.Listener<JSONArray>() {
  108. @Override
  109. public void onResponse(JSONArray response) {
  110. numData = response.length();
  111. Log.d("DEBUG_", "Parse JSON");
  112. latLng = new LatLng[numData];
  113. markerD = new Boolean[numData];
  114. nama = new String[numData];
  115. alamat = new String[numData];
  116. harga = new String[numData];
  117. jam = new String[numData];
  118. telepon = new String[numData];
  119. komentar = new String[numData];
  120. informasi = new String[numData];
  121. fasilitas = new String[numData];
  122. id = new String[numData];
  123. gambar = new String[numData];
  124. latitude = new Double[numData];
  125. longitude = new Double[numData];
  126.  
  127. for (int i = 0; i < numData; i++) {
  128. try {
  129. JSONObject data = response.getJSONObject(i);
  130. id[i] = data.getString("id");
  131. latLng[i] = new LatLng(data.getDouble("latitude"),
  132. data.getDouble("longitude"));
  133. nama[i] = data.getString("nama");
  134. alamat[i] = data.getString("alamat");
  135. harga[i] = data.getString("harga");
  136. jam[i] = data.getString("jam");
  137. telepon[i] = data.getString("telepon");
  138. komentar[i] = data.getString("komentar");
  139. informasi[i] = data.getString("informasi");
  140. fasilitas[i] = data.getString("fasilitas");
  141.  
  142. gambar[i] = data.getString("gambar");
  143. latitude[i] = data.getDouble("latitude");
  144. longitude[i] = data.getDouble("longitude");
  145.  
  146.  
  147. markerD[i] = false;
  148. mMap.addMarker(new MarkerOptions()
  149. .position(latLng[i])
  150. .title(nama[i])
  151. .snippet(alamat[i]));
  152. } catch (JSONException ignored) {
  153. }
  154. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng[i], 15.5f));
  155. }
  156.  
  157.  
  158. //----------------------MARKER KLIK
  159. mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
  160. @Override
  161. public boolean onMarkerClick(Marker marker) {
  162. Log.d("DEBUG_", "Marker clicked");
  163. for (int i = 0; i < numData; i++) {
  164.  
  165. if (marker.getTitle().equals(nama[i])) {
  166. if (markerD[i]) {
  167. Log.d("DEBUG_", "panggil activity");
  168. DetailSekolah.id = id[i];
  169. DetailSekolah.namaStudio = nama[i];
  170. DetailSekolah.alamat = alamat[i];
  171. DetailSekolah.harga = harga[i];
  172. DetailSekolah.jam = jam[i];
  173. DetailSekolah.callSave = telepon[i];
  174. DetailSekolah.informasi = informasi[i];
  175. DetailSekolah.fasilitas = fasilitas[i];
  176. DetailSekolah.komentar = komentar[i];
  177. DetailSekolah.gambar = gambar[i];
  178. DetailSekolah.latitude = latitude[i];
  179. DetailSekolah.longitude = longitude[i];
  180.  
  181. Intent intent = new Intent(MapsActivity.this, DetailSekolah.class);
  182. startActivity(intent);
  183. markerD[i] = false;
  184. } else {
  185. Log.d("DEBUG_", "show info");
  186. markerD[i] = true;
  187. marker.showInfoWindow();
  188. Toast ts = Toast.makeText(MapsActivity.this, "PENCET LAGI MARKER UNTUK DETAIL SEKOLAH", Toast.LENGTH_LONG);
  189. TextView v = (TextView) ts.getView().findViewById(android.R.id.message);
  190. if (v != null)
  191. v.setGravity(Gravity.CENTER);
  192. ts.show();
  193. }
  194. } else {
  195. markerD[i] = false;
  196. }
  197. }
  198. return false;
  199. }
  200.  
  201. });
  202. }
  203.  
  204.  
  205. }, new Response.ErrorListener() {
  206. @Override
  207. public void onErrorResponse(VolleyError error) {
  208. AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
  209. builder.setTitle("Error!");
  210. builder.setMessage("No Internet Connection");
  211. builder.setIcon(android.R.drawable.ic_dialog_alert);
  212. builder.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
  213. @Override
  214. public void onClick(DialogInterface dialog, int which) {
  215. getLokasi();
  216. }
  217. });
  218. AlertDialog alert = builder.create();
  219. alert.show();
  220. }
  221. });
  222. Volley.newRequestQueue(this).add(request);
  223. }
  224.  
  225. /**
  226. * Manipulates the map once available.
  227. * This callback is triggered when the map is ready to be used.
  228. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  229. * we just add a marker near Sydney, Australia.
  230. * If Google Play services is not installed on the device, the user will be prompted to install
  231. * it inside the SupportMapFragment. This method will only be triggered once the user has
  232. * installed Google Play services and returned to the app.
  233. */
  234. @Override
  235. public void onMapReady(GoogleMap googleMap) {
  236. mMap = googleMap;
  237.  
  238. mMap.getUiSettings().setZoomControlsEnabled(true);
  239. mMap.getUiSettings().setCompassEnabled(true);
  240. mMap.getUiSettings().setMapToolbarEnabled(true);
  241. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  242. // TODO: Consider calling
  243. // ActivityCompat#requestPermissions
  244. // here to request the missing permissions, and then overriding
  245. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  246. // int[] grantResults)
  247. // to handle the case where the user grants the permission. See the documentation
  248. // for ActivityCompat#requestPermissions for more details.
  249. return;
  250. }
  251. mMap.setMyLocationEnabled(true);
  252. // Add a marker in Sydney and move the camera
  253.  
  254. }
  255.  
  256. @Override
  257. public void onClick(View view) {
  258. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  259. assert imm != null;
  260. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  261. ambilcari = acTextView.getText().toString();
  262. lokasi();
  263. }
  264.  
  265. private void lokasi() {
  266. dialog.setMessage("Loading...");
  267. dialog.show();
  268.  
  269. String url = "https://arizalway.000webhostapp.com/api/lokasi.php?nama="+ambilcari;
  270. url = url.replaceAll(" ", "%20");
  271. System.out.println(url);
  272.  
  273.  
  274.  
  275.  
  276. StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
  277.  
  278.  
  279. @Override
  280. public void onResponse(String response) {
  281. Log.e("Response: ", response);
  282.  
  283. try {
  284. JSONArray jArray = new JSONArray(response);
  285.  
  286. for (int i = 0; i < jArray.length(); i++) {
  287. JSONObject jsonObject = jArray.getJSONObject(i);
  288.  
  289.  
  290. System.out.println(Arrays.toString(latLng));
  291. lati = Double.parseDouble(jsonObject.getString(LAT));
  292. longi = Double.parseDouble(jsonObject.getString(LNG));
  293. if (ambilcari.equals("")) {
  294.  
  295.  
  296. center = new LatLng(-7.568737, 110.827062);
  297. cameraPosition = new CameraPosition.Builder().target(center).zoom(15).build();
  298. gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  299. getLokasi();
  300.  
  301. } else {
  302. center = new LatLng(lati, longi);
  303. cameraPosition = new CameraPosition.Builder().target(center).zoom(20).build();
  304. gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  305.  
  306.  
  307. getLokasi();
  308. }
  309. }
  310. dialog.dismiss();
  311. } catch (JSONException e) {
  312. // JSON error
  313.  
  314. e.printStackTrace();
  315. }
  316.  
  317. }
  318.  
  319. }, new Response.ErrorListener() {
  320.  
  321. @Override
  322. public void onErrorResponse( VolleyError volleyError ) {
  323. try {
  324. String responseBody = new String( volleyError.networkResponse.data, "utf-8" );
  325. JSONObject jsonObject = new JSONObject( responseBody );
  326. } catch ( JSONException e ) {
  327. //Handle a malformed json response
  328. } catch (UnsupportedEncodingException ignored){
  329.  
  330. }
  331. }
  332. }
  333. );
  334.  
  335. AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
  336. }
  337.  
  338.  
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement