Guest User

Untitled

a guest
Mar 22nd, 2018
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. private GoogleMap mMap;
  2. double latitude;
  3. double longitude;
  4. private GoogleApiClient client;
  5. private LocationRequest locationRequest;
  6. private Location lastLocation;
  7. private Marker currentLocationMarker;
  8. public static final int REQUEST_LOCATION_CODE = 99;
  9. private int PROXIMITY_RADIUS = 10000;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_maps);
  15.  
  16. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  17. {
  18. checkLocationPermission();
  19. }
  20.  
  21. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  22. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  23. .findFragmentById(R.id.map);
  24. mapFragment.getMapAsync(this);
  25. Button btnRestaurant = (Button) findViewById(R.id.btnRestaurant);
  26. btnRestaurant.setOnClickListener(new View.OnClickListener() {
  27. String Restaurant = "restaurant";
  28. @Override
  29. public void onClick(View v) {
  30. Log.d("onClick", "Button is Clicked");
  31. mMap.clear();
  32. String url = getUrl(latitude, longitude, Restaurant);
  33. Object[] DataTransfer = new Object[2];
  34. DataTransfer[0] = mMap;
  35. DataTransfer[1] = url;
  36. Log.d("onClick", url);
  37. GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
  38. getNearbyPlacesData.execute(DataTransfer);
  39. Toast.makeText(MapsActivity.this,"Nearby Restaurants", Toast.LENGTH_LONG).show();
  40. }
  41. });
  42. Button btnHospital = (Button) findViewById(R.id.btnHospital);
  43. btnHospital.setOnClickListener(new View.OnClickListener() {
  44. String Hospital = "hospital";
  45. @Override
  46. public void onClick(View v) {
  47. Log.d("onClick", "Button is Clicked");
  48. mMap.clear();
  49. String url = getUrl(latitude, longitude, Hospital);
  50. Object[] DataTransfer = new Object[2];
  51. DataTransfer[0] = mMap;
  52. DataTransfer[1] = url;
  53. Log.d("onClick", url);
  54. GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
  55. getNearbyPlacesData.execute(DataTransfer);
  56. Toast.makeText(MapsActivity.this,"Nearby Hospitals", Toast.LENGTH_LONG).show();
  57. }
  58. });
  59. Button btnSchool = (Button) findViewById(R.id.btnSchool);
  60. btnSchool.setOnClickListener(new View.OnClickListener() {
  61. String School = "school";
  62. @Override
  63. public void onClick(View v) {
  64. Log.d("onClick", "Button is Clicked");
  65. mMap.clear();
  66. String url = getUrl(latitude, longitude, School);
  67. Object[] DataTransfer = new Object[2];
  68. DataTransfer[0] = mMap;
  69. DataTransfer[1] = url;
  70. Log.d("onClick", url);
  71. GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
  72. getNearbyPlacesData.execute(DataTransfer);
  73. Toast.makeText(MapsActivity.this,"Nearby Schools", Toast.LENGTH_LONG).show();
  74. }
  75. });
  76. }
  77. private boolean CheckGooglePlayServices() {
  78. GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
  79. int result = googleAPI.isGooglePlayServicesAvailable(this);
  80. if(result != ConnectionResult.SUCCESS) {
  81. if(googleAPI.isUserResolvableError(result)) {
  82. googleAPI.getErrorDialog(this, result,
  83. 0).show();
  84. }
  85. return false;
  86. }
  87. return true;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. @Override
  95. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  96. switch (requestCode)
  97. {
  98. case REQUEST_LOCATION_CODE:
  99. if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  100. {
  101. //permission is granted
  102. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
  103. {
  104. if(client != null)
  105. {
  106. buildGoogleApiClient();
  107. }
  108. mMap.setMyLocationEnabled(true);
  109. }
  110. }
  111. else //permission is denied
  112. {
  113. Toast.makeText(this, "Permission Denied!" , Toast.LENGTH_LONG).show();
  114. }
  115. return;
  116. }
  117. }
  118.  
  119.  
  120. /**
  121. * Manipulates the map once available.
  122. * This callback is triggered when the map is ready to be used.
  123. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  124. * we just add a marker near Sydney, Australia.
  125. * If Google Play services is not installed on the device, the user will be prompted to install
  126. * it inside the SupportMapFragment. This method will only be triggered once the user has
  127. * installed Google Play services and returned to the app.
  128. */
  129. @Override
  130. public void onMapReady(GoogleMap googleMap) {
  131. mMap = googleMap;
  132.  
  133. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  134. buildGoogleApiClient();
  135. mMap.setMyLocationEnabled(true);
  136. }
  137.  
  138. }
  139.  
  140. protected synchronized void buildGoogleApiClient()
  141. {
  142. client = new GoogleApiClient.Builder(this)
  143. .addConnectionCallbacks(this)
  144. .addOnConnectionFailedListener(this)
  145. .addApi(LocationServices.API)
  146. .build();
  147.  
  148. client.connect();
  149. }
  150.  
  151. @Override
  152. public void onLocationChanged(Location location) {
  153. lastLocation = location;
  154.  
  155. if(currentLocationMarker !=null)
  156. {
  157. currentLocationMarker.remove();
  158. }
  159.  
  160. LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  161.  
  162. MarkerOptions markerOptions = new MarkerOptions();
  163. markerOptions.position(latLng);
  164. markerOptions.title("Current Location");
  165. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  166.  
  167. currentLocationMarker = mMap.addMarker(markerOptions);
  168.  
  169. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  170. mMap.animateCamera(CameraUpdateFactory.zoomBy(10));
  171.  
  172. if(client != null)
  173. {
  174. LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
  175. }
  176.  
  177. }
  178.  
  179. @Override
  180. public void onConnected(@Nullable Bundle bundle) {
  181. locationRequest = new LocationRequest();
  182.  
  183. locationRequest.setInterval(1000);
  184. locationRequest.setFastestInterval(1000);
  185. locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  186.  
  187.  
  188. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
  189. LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
  190.  
  191. }
  192. private String getUrl(double latitude, double longitude, String nearbyPlace) {
  193.  
  194. StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
  195. googlePlacesUrl.append("location=" + latitude + "," + longitude);
  196. googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
  197. googlePlacesUrl.append("&type=" + nearbyPlace);
  198. googlePlacesUrl.append("&sensor=true");
  199. googlePlacesUrl.append("&key=" + "AIzaSyATA3OL5hATOCnGWY8INWycPhND6bIp-BU");
  200. Log.d("getUrl", googlePlacesUrl.toString());
  201. return (googlePlacesUrl.toString());
  202. }
  203.  
  204.  
  205. public boolean checkLocationPermission(){
  206.  
  207. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  208. {
  209. if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
  210. {
  211. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
  212. }
  213. else
  214. {
  215. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
  216. }
  217. return false;
  218. }
  219. else
  220. return true;
  221. }
  222.  
  223.  
  224.  
  225. @Override
  226. public void onConnectionSuspended(int i) {
  227.  
  228. }
  229.  
  230. @Override
  231. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  232.  
  233. }
  234.  
  235. <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  236. xmlns:map="http://schemas.android.com/apk/res-auto"
  237. xmlns:tools="http://schemas.android.com/tools"
  238. android:id="@+id/map"
  239. android:name="com.google.android.gms.maps.SupportMapFragment"
  240. android:layout_width="350dp"
  241. android:layout_height="500dp"
  242. tools:context="com.neilgmail.mita.icanemapp211.MapsActivity">
  243.  
  244. <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
  245. android:layout_height="125dp"
  246. android:layout_width="75dp">
  247.  
  248. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  249. xmlns:tools="https://schemas.android.com/tools"
  250. android:layout_height="75dp"
  251. android:layout_width="100dp"
  252. android:orientation="vertical">
  253.  
  254. <Button
  255. android:id="@+id/btnRestaurant"
  256. android:layout_height="10dp"
  257. android:layout_width="10dp"
  258. android:text="Nearby Restaurants"
  259. android:visibility="visible" />
  260.  
  261. <Button
  262. android:id="@+id/btnHospital"
  263. android:layout_height="10dp"
  264. android:layout_width="10dp"
  265. android:text="Nearby Hospitals"
  266. android:visibility="visible" />
  267.  
  268. <Button
  269. android:id="@+id/btnSchool"
  270. android:layout_height="10dp"
  271. android:layout_width="10dp"
  272. android:text="Nearby Schools"
  273. android:visibility="visible" />
  274.  
  275. </LinearLayout>
  276. </FrameLayout>
  277.  
  278. import android.Manifest;
  279. import android.content.pm.PackageManager;
  280. import android.location.Location;
  281.  
  282. import android.os.Build;
  283. import android.support.annotation.NonNull;
  284. import android.support.annotation.Nullable;
  285. import android.support.v4.app.ActivityCompat;
  286. import android.support.v4.app.FragmentActivity;
  287. import android.os.Bundle;
  288. import android.support.v4.content.ContextCompat;
  289. import android.util.Log;
  290. import android.view.View;
  291. import android.widget.Button;
  292. import android.widget.Toast;
  293.  
  294. import com.google.android.gms.common.ConnectionResult;
  295. import com.google.android.gms.common.GoogleApiAvailability;
  296. import com.google.android.gms.location.LocationListener;
  297. import com.google.android.gms.common.api.GoogleApiClient;
  298. import com.google.android.gms.location.LocationRequest;
  299. import com.google.android.gms.location.LocationServices;
  300. import com.google.android.gms.maps.CameraUpdateFactory;
  301. import com.google.android.gms.maps.GoogleMap;
  302. import com.google.android.gms.maps.OnMapReadyCallback;
  303. import com.google.android.gms.maps.SupportMapFragment;
  304. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  305. import com.google.android.gms.maps.model.LatLng;
  306. import com.google.android.gms.maps.model.Marker;
  307. import com.google.android.gms.maps.model.MarkerOptions;
  308.  
  309. public class MapsActivity extends FragmentActivity implements
  310. OnMapReadyCallback,
  311. GoogleApiClient.ConnectionCallbacks,
  312. GoogleApiClient.OnConnectionFailedListener,
  313. LocationListener {
Add Comment
Please, Sign In to add comment