2

I want to get the user's current location in map I cannot pass 'this' as a parameter in mapAsync function I am getting an error in getMapAsync(this). It tells Incompatible type.

required: com.google.android.gms.map.GoogleMap

found: void how can I solve this issue ?

I also tried getActivity.getApplicationContext() but it also not worked ! Here I am provide code please check it

 public class MapsActivity extends Fragment implements
    OnMapReadyCallback,
    View.OnClickListener ,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private GoogleMap map;
private MapView mapView;
private boolean mapsSupported = true;
private GoogleApiClient mGoogleApiClient;
MapFragment mapFragment;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    MapsInitializer.initialize(getActivity());

    if (mapView != null) {
        mapView.onCreate(savedInstanceState);
    }
    initializeMap();
}
private void initializeMap() {
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.map);
    //mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    if (map == null) {
        **map = mapFragment.getMapAsync(this);**
    }
    // Enable Zoom
    map.getUiSettings().setZoomGesturesEnabled(true);
    //set Map TYPE
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //enable Current location Button
    map.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 2000, 0, this);
}

Maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="@layout/abc_action_menu_layout"/>

<Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nearby Resturants"
    android:id="@+id/btn_rest"
    android:layout_gravity="bottom" />


   </RelativeLayout>
PulkitG
  • 612
  • 4
  • 12
SFAH
  • 624
  • 15
  • 35

3 Answers3

3

call your code inside Fragment onCreateView()

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

    View viewOBJ= inflater.inflate(R.layout.xxx, null, false);

     SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
     return viewOBJ;
}

For more information read getMapAsync() in Fragment

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

Try this code

try {
        if (map == null) {
            ((MapView) getView().findViewById(R.id.map)).getMapAsync(this);

        }
    }catch (NullPointerException e){
        e.printStackTrace();
    }

Hope this helps!

Tahmid Rahman
  • 748
  • 1
  • 8
  • 20
  • Glad to know! please mark it accepted (the tick mark at the left of the answer) so that it can help other developers in future. – Tahmid Rahman Feb 16 '17 at 06:24
1

What you're missing is that getMapAsync is an asynchronous function. meaning it does not return a map object right away. So you cannot just write map = mapFragment.getMapAsync();

Instead you have to do this :

private void initializeMap() {
  SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
        .findFragmentById(R.id.map);
  if (map == null) {
    mapFragment.getMapAsync(this);
  }
}

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

    // Enable Zoom
    map.getUiSettings().setZoomGesturesEnabled(true);
    //set Map TYPE
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //enable Current location Button
    map.setMyLocationEnabled(true);

    //... rest of your map related code.
}

Here, onMapReady is called when the map object s available, so the rest of your initialization code will go there. You also have to implement OnMapReadyCallback, but I see you've already done that.

ShahiM
  • 3,179
  • 1
  • 33
  • 58