Aspicas
Aspicas

Reputation: 4497

Google Maps v2- Cannot resolve method getSupportFragmentManager() on non-Activity class

I'm trying create non-Activity class to show my current location, get latitude/longitude, show markers....on Google Maps V2but always its reports an error:

Cannot resolve method getSupportFragmentManager()

I tried:

Part of My code

public final class AddDataGMapsV2 {

    private static Context context;

    public AddDataGMapsV2(Context context) {
        this.context = context;
    }


    public void showMap(SupportMapFragment map, GoogleMap googleMap) {


        //show map
        //Line error below
        map = (SupportMapFragment) context.getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = map.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

    }
    //.......................

Any suggestions?

Upvotes: 1

Views: 310

Answers (2)

user5113937
user5113937

Reputation:

The Context doesn't have the getSupportFragmentManager(). You need to use the Activity class:

map = (SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.map);

Upvotes: 0

Aspicas
Aspicas

Reputation: 4497

SOLUTION

I find the solution to my question, only deleting that line:

 map = (SupportMapFragment) context.getSupportFragmentManager().findFragmentById(R.id.map);

And later declare on my principal activity...Simple.

Upvotes: 1

Related Questions