Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package com.example.allen.routemaker;
  2.  
  3. import com.google.android.gms.common.ConnectionResult;
  4. import com.google.android.gms.common.api.GoogleApiClient;
  5. import com.google.android.gms.identity.intents.Address;
  6. import com.google.android.gms.location.LocationServices;
  7. import com.google.android.gms.maps.GoogleMap;
  8. import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
  9. import com.google.android.gms.maps.LocationSource;
  10. import com.google.android.gms.maps.OnMapReadyCallback;
  11. import com.google.android.gms.maps.SupportMapFragment;
  12. import com.google.android.gms.maps.model.LatLng;
  13.  
  14. import android.app.IntentService;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.content.pm.PackageManager;
  18. import android.location.Geocoder;
  19. import android.location.Location;
  20. import android.os.Bundle;
  21. import android.os.Handler;
  22. import android.os.Parcelable;
  23. import android.os.ResultReceiver;
  24. import android.support.annotation.NonNull;
  25. import android.support.annotation.Nullable;
  26. import android.support.v4.content.ContextCompat;
  27. import android.support.v7.app.AppCompatActivity;
  28. import android.util.Log;
  29. import android.view.View;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32.  
  33. import static com.example.allen.routemaker.FetchAddressIntentService.*;
  34. import static java.security.AccessController.getContext;
  35.  
  36. public class MapsActivity extends AppCompatActivity implements
  37.         OnMapReadyCallback {
  38.  
  39.     private LongPressLocationSource mLocationSource;
  40.     private AddressResultReceiver mResultReceiver;
  41.     private String TAG = "allenMessage";
  42.     public static String mAddressOutput;
  43.  
  44.  
  45.     private boolean checkLocationPermission() {
  46.         if (ContextCompat.checkSelfPermission(this,
  47.                 android.Manifest.permission.ACCESS_FINE_LOCATION)
  48.                 != PackageManager.PERMISSION_GRANTED) {
  49.             return false;
  50.         } else {
  51.             return true;
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     protected void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.activity_maps);
  59.  
  60.         mLocationSource = new LongPressLocationSource();
  61.         mResultReceiver = new AddressResultReceiver(null);
  62.  
  63.         startIntentService();
  64.  
  65.         SupportMapFragment mapFragment =
  66.                 (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  67.         mapFragment.getMapAsync(this);
  68.     }
  69.  
  70.  
  71.     @Override
  72.     public void onMapReady(GoogleMap map) {
  73.         map.setLocationSource(mLocationSource);
  74.         map.setOnMapLongClickListener(mLocationSource);
  75.         checkLocationPermission();
  76.         map.setMyLocationEnabled(true);
  77.     }
  78.  
  79.     protected void startIntentService() {
  80.         Intent intent = new Intent(this, FetchAddressIntentService.class);
  81.         intent.putExtra(Constants.RECEIVER, mResultReceiver);
  82.         intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLocationSource.getLocation());
  83.         startService(intent);
  84.     }
  85.  
  86.  
  87.     private class AddressResultReceiver extends ResultReceiver {
  88.  
  89.         AddressResultReceiver(Handler handler) {
  90.             super(handler);
  91.         }
  92.  
  93.         @Override
  94.         protected void onReceiveResult(int resultCode, Bundle resultData) {
  95.             mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
  96.  
  97.             showToast(mAddressOutput);
  98.         }
  99.     }
  100.  
  101.     public void showToast(String address){
  102.         CharSequence add = address;
  103.         Toast toast = Toast.makeText(this,add,Toast.LENGTH_LONG);
  104.         toast.show();
  105.         Log.i(TAG, address);
  106.     }
  107.  
  108.     public void SaveAddress(View view) {
  109.         startIntentService();
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement