Search code examples
androidgoogle-mapsgoogle-geocoder

Does Geocoder actually work with google map api v2?


i am try to use Geocoder with my google map. I can get the latitude and longitude from Location name. My problem is that I can not use the latitude and longitude to add any marker or move the camera to that position. Every time it shows null pointer exception. But I get the latitude and longitude as text view. I have used all the permission, actually Geocoder works fine but not with the google map. Doesn't Geocoder work to add any marker or move to that place on google map?

I am using fragment class because of navigation drawer , does it have anything to create this problem?

If any body has any answer, that would be helpful. I can post my work if anyone asks. Thank you

this is my fragment class

public class GMapFragment extends Fragment implements OnMapReadyCallback{
    private GoogleMap googleMap;
    MapFragment fragment;
    EditText location_tf;
    Button button;
    List<Address> addressList;
    Geocoder geocoder;
    double lat;
    double lng;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_gmap, container, false);
            location_tf = (EditText)rootView.findViewById (R.id.TFaddress);

            button = (Button)rootView.findViewById(R.id.Bsearch);
            button.setOnClickListener(buttonOnClickListener);

            initilizeMap();
            return rootView;}



                OnClickListener buttonOnClickListener =
                           new OnClickListener(){

        public void onClick(View v)

        {



            String location = location_tf.getText().toString();
            Geocoder geocoder = new Geocoder(getActivity(),Locale.ENGLISH);
            List<Address> addressList = null;
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Address address = addressList.get(0);
          String locality= address.getLocality();
          Toast.makeText(getActivity(), locality, Toast.LENGTH_SHORT).show();

          lat = address.getLatitude();
          lng = address.getLongitude();

          LatLng ll = new LatLng(lat, lng);
          googleMap.addMarker(new MarkerOptions()       //line 89
            .position((ll)));
          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    (ll), 15));


        }
                }
         ;
         private void initilizeMap()
        {
            MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
               if (fragment != null){
                   fragment.getMapAsync(this);

               }

        }
        @Override
        public void onMapReady(final GoogleMap googleMap) {
            googleMap.setMyLocationEnabled(true);
            googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }

         @Override
        public void onPause() {

            final FragmentManager fragManager = this.getFragmentManager();
            final Fragment fragment = fragManager.findFragmentById(R.id.map);
            if(fragment!=null){
                fragManager.beginTransaction().remove( fragment).commit();
            super.onPause();

            }
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            final FragmentManager fragManager = this.getFragmentManager();
            final Fragment fragment = fragManager.findFragmentById(R.id.map);
            if(fragment!=null){
                fragManager.beginTransaction().remove(fragment).commit();
            }
        }
        }

this is my LogCat

05-23 20:05:36.133: E/AndroidRuntime(10092): FATAL EXCEPTION: main
05-23 20:05:36.133: E/AndroidRuntime(10092): Process: com.cubecode.mapdhaka, PID: 10092
05-23 20:05:36.133: E/AndroidRuntime(10092): java.lang.NullPointerException
05-23 20:05:36.133: E/AndroidRuntime(10092):    at    com.cubecode.mapdhaka.GMapFragment$1.onClick(GMapFragment.java:89)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.view.View.performClick(View.java:4472)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.view.View$PerformClick.run(View.java:18779)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.os.Handler.handleCallback(Handler.java:808)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.os.Handler.dispatchMessage(Handler.java:103)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.os.Looper.loop(Looper.java:193)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at android.app.ActivityThread.main(ActivityThread.java:5292)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at java.lang.reflect.Method.invoke(Method.java:515)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
05-23 20:05:36.133: E/AndroidRuntime(10092):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • googleMap variable is null because you never assign the variable.

    Add onMapReady:

    this.googleMap = googleMap;