简体   繁体   中英

Android Google Map v2 - Start new Activity by clicking somewhere on the GoogleMap

This is my situation: I have an Activity which reads the current Location, shows the Address in a TextView and sets a Marker on a "small" GoogleMap" at the bottom area of the Activity. At this map ScrollGestures are disabled - it's just to show the current location to the user.

IF the currect Location is not correct the User should be able to click somewhere on the map, which opens a new activity where he can drag the marker to his correct position.

My question is: How can i start a new Activity by clicking somewhere on the map.

I tried this, but i was not able to put an Intent in the onClick method: a link !

Here is my code:

    private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();
        //googleMap.setMyLocationEnabled(true);

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                lat, lng), 13));
        googleMap.getUiSettings().setScrollGesturesEnabled(false);
        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng arg0) {

                Intent intent2 = new Intent(this, SettingsActivity.class);
                startActivity(intent2);
            }
        });
        addMarker();

        }
    }
}

When you use this in your call to startActivity , you are not referencing a Context as you should be. Instead, you are referencing the instance of the anonymous inner class you have created for the onMapClickListener . You either need to replace this with YourActivityName.this or with YourFragmentName.this.getActivity() depending on what the outer class is (seems like it's a Fragment ...)

Please more clear about what error you are receiving next time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM