1

this is my code for now

    public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

        private GoogleMap map;
        Marker marker;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);




        }

        @Override
        public void onMapReady(GoogleMap googleMap) {

            map = googleMap;

            setUpMap();


        }

        public void setUpMap() {

            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            map.setMyLocationEnabled(true);
        }

        void getCurrentLocation()
        {
            Location myLocation  = map.getMyLocation();
            if(myLocation!=null)
            {
                double dLatitude = myLocation.getLatitude();
                double dLongitude = myLocation.getLongitude();
                map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                        .title("My Location").icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

            }
            else
            {
                Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
            }
        }
}

and I already tried many options like onTouch onMapClickListeners etc, and nothing work for me, i think that im doing someting wrong, this is my first googleMap project and dosent know how to add marker when i click on map. I will need it later to get Address and im asking for help. In documentary is only with static values but i need dynamic.

Edit

        map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(point.latitude, point.longitude)).title("You are here");

            map.clear();
            map.addMarker(marker);
        }
    });

After i add someting like that i get that kind of error and how could i solve it ?

void com.google.android.gms.maps.GoogleMap.setOnMapClickListener(com.google.android.gms.maps.GoogleMap$OnMapClickListener) on a null object reference

Kertuj
  • 33
  • 5

0 Answers0