0

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 {
        }
    }
}
Community
  • 1
  • 1
  • 1
    the code seems to be ok, maybe you can't see the line due to the camera zoom? are you sure that if you zoom out to world view you can't see the line? Even more, are you sure code is called (debug mode)? – N Dorigatti Sep 21 '16 at 09:07
  • Even if I zoom out I still don't see it. Plus the line starts at my current location anyways. The code should be called since everything after `line= mMap.addPolyline` is called. – James Robert Singleton Sep 21 '16 at 15:27
  • I feel stupid, I googled coordinates and didn't think to put a - sign in front of the 118's. – James Robert Singleton Sep 21 '16 at 15:43
  • 1
    Well, you are not forced but it will draw the line in Asia rather than us... Thats why i told you to zoom world Wide! – N Dorigatti Sep 21 '16 at 16:02
  • Haha well I needed it in the US haha. What is weird is when I google Lat and Long for the area it gives 33.9192° N, 118.4165° W. Forgot to translate it to have a -118.4165 haha – James Robert Singleton Sep 21 '16 at 17:20
  • What is weird, is that when I add multiple .add the line no longer displays – James Robert Singleton Sep 22 '16 at 03:40
  • You should use addall – N Dorigatti Sep 22 '16 at 05:07
  • Do you have an example? I just did .add() then put a whole bunch of lat longs in there. Do you know how long of a number new LatLng accepts? The reason I ask is because I put in values like such (33.916579890000001, -118.3074421) but it doesn't draw even a small line. – James Robert Singleton Sep 22 '16 at 05:32

1 Answers1

1

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).

N Dorigatti
  • 3,482
  • 2
  • 22
  • 33
  • Hmm interesting. Yea the lat and long that I am using is really precise, so I guess it would take a lot more than 4-5 to see a line since they get cut down to the 5th number. – James Robert Singleton Sep 22 '16 at 19:56
  • well wait, the 4-5 decimals are per-point, if you cut to the 5th decimal and have different values than the line must be drawn! Is it working now? If not please update your code, I'm lost now :D – N Dorigatti Sep 22 '16 at 20:44
  • 1
    So I updated it haha, it is such a small dot when you are at the zoom that I am at....I guess I should make the line thicker. – James Robert Singleton Sep 23 '16 at 02:27