SHARE
TWEET

Untitled

a guest Jun 25th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.lurntech.ahassiap;
  2.  
  3. import android.location.Address;
  4. import android.location.Geocoder;
  5. import android.os.Bundle;
  6. import android.support.v4.app.Fragment;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.SearchView;
  11.  
  12. import com.google.android.gms.maps.CameraUpdateFactory;
  13. import com.google.android.gms.maps.GoogleMap;
  14. import com.google.android.gms.maps.OnMapReadyCallback;
  15. import com.google.android.gms.maps.SupportMapFragment;
  16. import com.google.android.gms.maps.model.LatLng;
  17. import com.google.android.gms.maps.model.MarkerOptions;
  18.  
  19. import java.io.IOException;
  20. import java.util.List;
  21. import java.util.Locale;
  22.  
  23. public class LocationFragment extends Fragment implements OnMapReadyCallback{
  24.  
  25.     View mView;
  26.     GoogleMap map;
  27.     SupportMapFragment mapFragment;
  28.     SearchView searchView;
  29.  
  30.     public LocationFragment(){
  31.     }
  32.  
  33.     @Override
  34.     public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  35.         mView = inflater.inflate( R.layout.fragment_location, container, false );
  36.  
  37.         searchView = mView.findViewById( R.id.sv_location );
  38.         mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById( R.id.google_map );
  39.         searchView.setOnQueryTextListener( new SearchView.OnQueryTextListener() {
  40.             @Override
  41.             public boolean onQueryTextSubmit(String query) {
  42.                 String location = searchView.getQuery().toString();
  43.                 List<Address> addressList = null;
  44.  
  45.                 if(location != null || !location.equals( "" )){
  46.                     Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); //error
  47.  
  48.                     try{
  49.                         addressList = geocoder.getFromLocationName( location, 16 );
  50.                     } catch (IOException e) {
  51.                         e.printStackTrace();
  52.                     }
  53.                     Address address = addressList.get( 0 ); // not String in address list if i input string
  54.                     LatLng latLng = new LatLng( address.getLatitude(), address.getLongitude() );
  55.                     map.addMarker( new MarkerOptions().position( latLng ).title( location ) );
  56.                     map.animateCamera( CameraUpdateFactory.newLatLngZoom( latLng, 10 ) );
  57.                 }
  58.  
  59.                 return false;
  60.             }
  61.  
  62.             @Override
  63.             public boolean onQueryTextChange(String newText) {
  64.                 return false;
  65.             }
  66.         } );
  67.  
  68.         mapFragment.getMapAsync( this);
  69.         return mView;
  70.     }
  71.  
  72.     @Override
  73.     public void onMapReady(GoogleMap googleMap) {
  74.         map = googleMap;
  75.     }
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top