James Robert Singleton
James Robert Singleton

Reputation: 461

Line not being drawn on Google Maps

I have google maps inside one of my Nav drawer fragments and don't want to switch it to activity. However, just to simply draw a line on the map, I added the following bit of code:

lineOptions = new PolylineOptions()
                .add(new LatLng(33.927085040000001, -118.39129683))
                .add(new LatLng(33.927085969382027, -118.39129820011991))
                .add(new LatLng(33.927081024586677, -118.39130352185116))
                .add(new LatLng(33.927077084498386, -118.39130986277655))
                .add(new LatLng(33.92707405365519, -118.39131496827862))
                .add(new LatLng(33.927066722586623, -118.39131750469446))
                .add(new LatLng(33.927068689880947, -118.39131823397425))
                .add(new LatLng(33.927068030419839, -118.39131796208994))
                .add(new LatLng(33.927065982966624, -118.39132090766913))
                .add(new LatLng(33.927065258415212, -118.3913193654964))
                .width(5)
                .color(Color.RED);
        line = mMap.addPolyline(lineOptions);

However, it isn't adding the line between those two points. I got that bit of code from this SO question Drawing a line/path on Google Maps and the person said it worked. So I am not sure where I am going wrong. The reason I have hard coded values in there is because after I figure this out, I will be plotting multiple routes from a JSON response. Here is all the code:

public class ThirdFragment extends Fragment implements OnMapReadyCallback {
    View myView;
    private GoogleMap mMap;
    MapFragment mapFrag;
    private Polyline line;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.third_layout, container, false);
        return myView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapFrag = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);
        //mapFrag.onResume();
    }

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


        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            line = mMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(33.8031, 118.0726), new LatLng(33.9192, 118.4165))
                    .width(5)
                    .color(Color.RED));
            mMap.setMyLocationEnabled(true);
            Criteria criteria = new Criteria();
            LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            String provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            double lat =  location.getLatitude();
            double lng = location.getLongitude();
            LatLng coordinate = new LatLng(lat, lng);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
            mMap.animateCamera(yourLocation);


        } else {
            final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create();

            alertDialogGPS.setTitle("Info");
            alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app.");
            alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
                    startActivity(intentSettings);
                    alertDialogGPS.dismiss();
                }

            });

            alertDialogGPS.show();
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            mapFrag.getMapAsync(this);
        }
        else {
        }
    }
}

Upvotes: 0

Views: 343

Answers (1)

N Dorigatti
N Dorigatti

Reputation: 3540

I think add with list is not the best method to call... directly from the official guides:

PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

As you can see, add each point with the add function. Regarding the size of long, you can put whatever you want, gmaps will cut the decimals where it wants (approx 5th number). Try with this, then try changing the coordinates. Remember Lat is the Y axis (africa is ~ negative lat, with values from -90 to 90) and Lon is the X axis (USA is negative lon, with values from -180 to 180).

Upvotes: 1

Related Questions