Skip to content Skip to sidebar Skip to footer

Polyline Not Visible In Google Maps

I am trying to add polyline connecting a group of points in google maps.When i build the app I am not getting any error but polyline does not appear.If i call isvisible() method it

Solution 1:

The issue is in

googleMap.setMyLocationEnabled(true);

line, because you probably didn't grant ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION to your app. You should grant it manually to your application via Applications menu of your Android device or implement checking and grant permissions like in Official Documentation and, for example, this answer of Daniel Nugent, something like that:

publicclassMainActivityextendsAppCompatActivityimplementsOnMapReadyCallback {

    privatestaticfinalintLOCATION_PERMISSION_REQUEST_CODE=101;
    private GoogleMap mGoogleMap;
    private MapFragment mMapFragment;

    privatevoidmakeLocationPermissionRequest() {
        ActivityCompat.requestPermissions(this,
                newString[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
    }

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mMapFragment.getMapAsync(this);
    }

    privatevoidshowPolyline() {

        mGoogleMap.setMyLocationEnabled(true);

        Polylinepolyline1= mGoogleMap.addPolyline(newPolylineOptions()
                .clickable(true)
                .add(
                        newLatLng(13.034063, 77.567006 ),
                        newLatLng(13.034087, 77.568629),
                        newLatLng(13.034202, 77.569431),
                        newLatLng(13.034371, 77.570103),
                        newLatLng(13.034535, 77.570121),
                        newLatLng(13.036526, 77.570489),
                        newLatLng(13.037719, 77.570545),
                        newLatLng(13.038252, 77.570039),
                        newLatLng(13.039849, 77.570028),
                        newLatLng(13.040153, 77.569229),
                        newLatLng(13.040640, 77.568512),
                        newLatLng(13.041071, 77.567996),
                        newLatLng(13.041780, 77.567894))
                .color(Color.GREEN)
                .width(66));

        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newLatLng(13.034063, 77.567006), 12));
    }

    @OverridepublicvoidonMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            intlocationPermission= ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
            if (locationPermission != PackageManager.PERMISSION_GRANTED) {
                makeLocationPermissionRequest();
            } else {
                showPolyline();
            }
        } else {
            showPolyline();
        }
}

    @OverridepublicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case LOCATION_PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    showPolyline();
                } else {
                }
                return;
            }
        }
    }

}

And don't forget to add

to your AndroidManifest.xml file. And you got something like that:

enter image description here

Post a Comment for "Polyline Not Visible In Google Maps"