Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private GoogleMap mMap;
- private GoogleApiClient googleApiClient;
- private LocationRequest locationRequest;
- private Location lastLocation;
- private Marker currentUserLocationMarker;
- private static final int Request_User_Location_Code=99;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_google_maps);
- if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
- checkUserLocationPermmision();
- }
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- public Boolean checkUserLocationPermmision(){
- 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}, Request_User_Location_Code );
- }
- else{
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code );
- }
- return false;
- }
- else{
- return true;
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- switch(requestCode){
- case Request_User_Location_Code:
- if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
- if(ContextCompat.checkSelfPermission
- (this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
- if( googleApiClient== null){
- buildGoogleApiClient();
- }
- mMap.setMyLocationEnabled(true);
- }
- else{
- Toast.makeText(this, "pemission denied ", Toast.LENGTH_SHORT).show();
- }
- return;
- }
- }
- }
- protected synchronized void buildGoogleApiClient(){
- googleApiClient= new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- googleApiClient.connect();
- }
- @Override
- public void onLocationChanged(Location location) {
- this.lastLocation= location;
- if(currentUserLocationMarker!= null){
- currentUserLocationMarker.remove();
- }
- LatLng latLng= new LatLng(location.getLatitude(),location.getLongitude());
- MarkerOptions markerOptions= new MarkerOptions();
- markerOptions.position(latLng);
- markerOptions.title("user current Location");
- markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
- currentUserLocationMarker= mMap.addMarker(markerOptions);
- mMap.moveCamera(CameraUpdateFactory.zoomBy(14));
- if(googleApiClient!= null){
- LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
- }
- }
- public void onClick(View v){
- switch(v.getId()){
- case R.id.search_address:
- EditText addressField=(EditText)findViewById(R.id.location_search);
- 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(BitmapDescriptorFactory.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;
- }
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- locationRequest= new LocationRequest();
- locationRequest.setInterval(1100);
- locationRequest.setFastestInterval(1100);
- locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
- LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest,this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement