Kamlesh
Kamlesh

Reputation: 31

getting error with getMap()?

I am trying for interactive infoWindow with one of the answer of stack overflow.

link is given below:

interactive infowindow

but i am getting error with getMap() used in the code. although I tried with getMapAsync but unable to resolve the problem. please help me asap.If any new code available for interactive infowindow with button then please share the code.

    public class MainActivity extends Activity {    
    private ViewGroup infoWindow;
    private TextView infoTitle;
    private TextView infoSnippet;
    private Button infoButton;
    private OnInfoWindowElemTouchListener infoButtonListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
        final GoogleMap map = mapFragment.getMap();

        // MapWrapperLayout initialization
        // 39 - default marker height
        // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge 
        mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20)); 

        // We want to reuse the info window for all the markers, 
        // so let's create only one class member instance
        this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.info_window, null);
        this.infoTitle = (TextView)infoWindow.findViewById(R.id.title);
        this.infoSnippet = (TextView)infoWindow.findViewById(R.id.snippet);
        this.infoButton = (Button)infoWindow.findViewById(R.id.button);

        // Setting custom OnTouchListener which deals with the pressed state
        // so it shows up 
        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
                getResources().getDrawable(R.drawable.btn_default_pressed_holo_light)) 
        {
            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // Here we can perform some action triggered after clicking the button
                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
            }
        }; 
        this.infoButton.setOnTouchListener(infoButtonListener);


        map.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Setting up the infoWindow with current's marker info
                infoTitle.setText(marker.getTitle());
                infoSnippet.setText(marker.getSnippet());
                infoButtonListener.setMarker(marker);

                // We must call this to set the current marker and infoWindow references
                // to the MapWrapperLayout
                mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                return infoWindow;
            }
        });

        // Let's add a couple of markers
        map.addMarker(new MarkerOptions()
            .title("Prague")
            .snippet("Czech Republic")
            .position(new LatLng(50.08, 14.43)));

        map.addMarker(new MarkerOptions()
            .title("Paris")
            .snippet("France")
            .position(new LatLng(48.86,2.33)));

        map.addMarker(new MarkerOptions()
            .title("London")
            .snippet("United Kingdom")
            .position(new LatLng(51.51,-0.1)));
    }

    public static int getPixelsFromDp(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dp * scale + 0.5f);
    }
}

error is coming because of google has removed getMap but dont know the alternative solution for this.

Error:(149, 42) error: cannot find symbol method getMap()

Upvotes: 1

Views: 517

Answers (3)

Deep Patel
Deep Patel

Reputation: 2644

Your Existing Code is:

 final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
    final GoogleMap map = mapFragment.getMap();

Try this instead of that:

 MapFragment mapFragment; // declare in global scope
 GoogleMap googleMap; // declare in global scope

Put this code in OnCreate method:

mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);

                mapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap map) {
                        googleMap = map;

                        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        googleMap.setMyLocationEnabled(false); // false to disable
                        googleMap.setOnMarkerClickListener(onMarkerClick);
                        googleMap.setOnInfoWindowClickListener(onInfoWindowClick);
                        googleMap.getUiSettings().setZoomControlsEnabled(true);// Display Zoom Controls
                        googleMap.setMyLocationEnabled(true);// Display My Location Control
                    }
                });
            }

for converting pixels into DP use this:

    public int getPixelsFromDp(int px){ 
       DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); 
    return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); 
} 

Upvotes: 2

kboskin
kboskin

Reputation: 420

Use getMapAsync in your onCreatView() method ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)) .getMapAsync(this)

Also imeplement the OnMapReadyCallback interface, and override it's method

In method onMapReady() which belongs to onMapReadyCallback interface do all the operations with your map you need. It will look like after your manipulations next

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    // some action with our map
    mMap.addMarker(new MarkerOptions().position(randomPosition).title("Random Title"));
    mMap.addMarker(new MarkerOptions().position(antoherRandomPosition).title("Another RND Title"));

    // some processes with db
    DataBaseMapFragment database = new DataBaseMapFragment(dbHelper, mMap);
    database.createBD();


    // permission to check, if gps access is granted
    if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.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;
    }

    //another actions with map
    mMap.setMyLocationEnabled(true);
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camera, 13));
    mMap.setBuildingsEnabled(true);
    mMap.setOnMapLongClickListener(this);
    mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    mMap.setOnMarkerClickListener(this);

reffering Google documentation and guide.

Check it to learn more about maps in android and to be clear about

Upvotes: 1

Thomas.L.T
Thomas.L.T

Reputation: 76

The getMap() function replaced from getMapAsync() more here

https://developers.google.com/android/reference/com/google/android/gms/maps/package-summary

Upvotes: 0

Related Questions