googlemap lite mode in mapview is not working in marshmallow

  • Replies:7
Pradeepkumar Reddy
  • Forum posts: 11

Aug 26, 2016, 9:13:33 AM via Website

=> I'm using following MapView in my layout:-

------------------------------------------------------------

android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_gravity="bottom"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/ivLocation"
android:padding="1dp"
android:background="#D3D3D3"
map:liteMode="true"
/>
=> I have the following code in onCreateView() of fragment:-


MapsInitializer.initialize(this.getActivity());
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
mapView.setClickable(false);

=> And i also have the following code in the fragment class :-


@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onDestroyView() {
super.onDestroyView();
}

@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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;
}
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng latLng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
map.addMarker(new MarkerOptions().position(latLng).title("City Coordinator location at login").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
map.getUiSettings().setMapToolbarEnabled(false);
}

This code is working in moto e2 phone with lollipop android, but not working in moto g3 phone with marshmallow android. In marshmallow, map is not loading at all in googlemap lite mode. If i remove this attribute( map:liteMode="true") in mapview, map is loading but marker is not present.

Reply
Mithu Roy
  • Forum posts: 3

Aug 27, 2016, 2:51:08 PM via Website

Add this code in your MainActivity, User Permission is required in Marshmallow devices. I hope it will work.

protected void requestPermission(String permissionType, int
    requestCode) {
    int permission = ContextCompat.checkSelfPermission(this,
    permissionType);
    if (permission != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
    new String[]{permissionType}, requestCode
    );
    }
    }

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[]
grantResults) {
switch (requestCode) {
case LOCATION_REQUEST_CODE: {
if (grantResults.length == 0
|| grantResults[0] != PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, “Unable to show location -
permission required”, Toast.LENGTH_LONG).show();
} return;
}
}
}

Reply
Pradeepkumar Reddy
  • Forum posts: 11

Aug 28, 2016, 8:47:30 AM via Website

what to pass as arguments for requestPermission(). What permissionType and requestCode should i pass.

Reply
Mithu Roy
  • Forum posts: 3

Aug 28, 2016, 8:52:15 AM via Website

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_demo);
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
LOCATION_REQUEST_CODE);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

I Hope it will work.

Reply
Pradeepkumar Reddy
  • Forum posts: 11

Aug 29, 2016, 6:08:31 PM via Website

It is working after enabling location permission for that app in phone settings.

Reply
Mithu Roy
  • Forum posts: 3

Aug 29, 2016, 6:48:50 PM via Website

In Android 6.0 Marshmallow you've to enable the permission manually or the app itself has to ask the permission from the user, the code I've provided is intended for taking authorization to allow the location permission in the device.

Reply
Pradeepkumar Reddy
  • Forum posts: 11

Aug 30, 2016, 4:41:56 AM via Website

in the code you have provided, I'm getting error in the following line, android studio is not able to resolve the symbol ACCESS_FINE_LOCATION.

requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_REQUEST_CODE);

Reply
damponting44
  • Forum posts: 70

Oct 6, 2016, 1:59:36 PM via Website

@Override

ensured void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_map_demo);

requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,

LOCATION_REQUEST_CODE);

SupportMapFragment mapFragment =

(SupportMapFragment) getSupportFragmentManager()

.findFragmentById(R.id.map);

mapFragment.getMapAsync(this);

}

I Hope it will work.

Reply