0

i am trying to draw polyline for googlemap, it is working fine upto android version 4.4(kitkat) but it is not working in android 5.0(lollipop). what to do for working in lollipop also. could you please help me hoe to resolve this issues this is my google map code

public class MapLocation extends FragmentActivity implements OnMarkerClickListener {

    GoogleMap map;
    ArrayList<LatLng> markerPoints;

    LatLng origin,dest1,dest2,dest3;    
    Marker marker1,marker2,marker3;

    PolylineOptions lineOptions = new PolylineOptions();

    Polyline line ;
    int pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.gmap_direction);

        markerPoints = new ArrayList<LatLng>();

        // Getting reference to SupportMapFragment of the activity_main
        SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();      

        if(map!=null){

            // Enable MyLocation Button in the Map
            map.setMyLocationEnabled(true);     

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);
            pos = getIntent().getIntExtra("pos",0); 
            try{
            switch (pos) {

               case 1:

                   dest2 = new LatLng(18.545981, 73.902132);
                   marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("PUNE,KALYANINAGAR").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 2:

                   dest2 = new LatLng(17.429122, 78.412745);
                   marker2 =  map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Jubilee Hills").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 3:

                   dest2 =  new LatLng(19.115177, 72.909486);
                   marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("MUMBAI, POWAI").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 4:

                   dest2 = new LatLng(18.545981, 73.902132);
                   marker2 =  map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("PUNE,MAGARPATTA").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 5:

                   dest2 = new LatLng(12.924438, 77.631537);
                   marker2 =  map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Koramangala").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 6:

                   dest2 =  new LatLng(12.9044019,77.6690773);
                   marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Sarjapur").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 7:

                   dest2 = new LatLng(12.972442, 77.639195);
                   marker2 =  map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Indiranagar").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
               case 8:

                   dest2 = new LatLng(12.968719, 77.747883);
                   marker2 =  map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Whitefield").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   break;
                   //  dest1 = new LatLng(19.115177, 72.909486);
            }

            // Showing the current location in Google Map
            map.moveCamera(CameraUpdateFactory.newLatLng(dest2));

            // Zoom in the Google Map
            map.animateCamera(CameraUpdateFactory.zoomTo(15));     

            marker2.showInfoWindow();   


            if(location!=null){
                onLocationChanged(location);

            String url = getDirectionsUrl(origin, dest2);               

            DownloadTask downloadTask = new DownloadTask();

            // Start downloading json data from Google Directions API
            downloadTask.execute(url);
            }
            }
            catch(Exception e){

            }
        }       
    }
    /** A class to parse the Google Places in JSON format */
    private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{

        // Parsing the data in non-ui thread        
        @Override
        protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

            JSONObject jObject; 
            List<List<HashMap<String, String>>> routes = null;                     

            try{
                jObject = new JSONObject(jsonData[0]);
                DirectionsJSONParser parser = new DirectionsJSONParser();

                // Starts parsing data
                routes = parser.parse(jObject);    
            }catch(Exception e){
                e.printStackTrace();
            }
            return routes;
        }

        // Executes in UI thread, after the parsing process
        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> result) {
            ArrayList<LatLng> points = null;
            //PolylineOptions lineOptions = null;

            MarkerOptions markerOptions = new MarkerOptions();

            // Traversing through all the routes
            for(int i=0;i<result.size();i++){
                points = new ArrayList<LatLng>();
                //lineOptions = new PolylineOptions();              

                // Fetching i-th route
                List<HashMap<String, String>> path = result.get(i);

                // Fetching all the points in i-th route
                for(int j=0;j<path.size();j++){
                    HashMap<String,String> point = path.get(j);                 

                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng position = new LatLng(lat, lng); 

                    points.add(position);                       
                }

                // Adding all the points in the route to LineOptions
                lineOptions.addAll(points);
                lineOptions.width(4);
                lineOptions.color(Color.BLUE);

            }

            // Drawing polyline in the Google Map for the i-th route
            //map.addPolyline(lineOptions); 
            line = map.addPolyline(lineOptions);
        }           
    }
} 
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
dawood rizwan
  • 183
  • 1
  • 5
  • 16
  • please describe your problem – Xcihnegn Feb 23 '15 at 08:59
  • @Xingchen i am displaying google map with marker and polyline it is working fine upto android 4.4 but android 5.0 poly line is not coming. poly line means for example i want to draw line from current location(gps) to destination location for that i am using poly line this is working upto 4.4 but not working in 5.0 this is my problem – dawood rizwan Feb 23 '15 at 10:33
  • you mean polyline not showing without any error? – Xcihnegn Feb 23 '15 at 10:36
  • @Xingchen i am not getting any error. my problem is poly line is not showing on android version 5.0(lollipop) only but it is showing android version 4.4(kitkat) could you please tell me how to resolve this – dawood rizwan Feb 23 '15 at 10:42
  • `addPolyLine` is from `Google Maps Android API v2`, so anything to do with Android API 21? – Xcihnegn Feb 23 '15 at 10:58
  • @Xingchen sorry i am not getting what you said. exactly now what i have to do for resolve this issues – dawood rizwan Feb 23 '15 at 11:01

2 Answers2

1

Try the modified codes for your onPostExecute:

@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        //PolylineOptions lineOptions = null;

        //MarkerOptions markerOptions = new MarkerOptions();

        points = new ArrayList<LatLng>();

        // Traversing through all the routes
        for(int i=0;i<result.size();i++){
            //points = new ArrayList<LatLng>();
            //lineOptions = new PolylineOptions();              

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for(int j=0;j<path.size();j++){
                HashMap<String,String> point = path.get(j);                 

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng); 

                points.add(position);                       
            }

            // Adding all the points in the route to LineOptions
            //lineOptions.addAll(points);
            //lineOptions.width(4);
            //lineOptions.color(Color.BLUE);

        }

        // Drawing polyline in the Google Map for the i-th route
        //map.addPolyline(lineOptions); 

        lineOptions.addAll(points);
        lineOptions.width(4);
        lineOptions.color(Color.BLUE); 
        line = map.addPolyline(lineOptions);
    }           

I moved your list points instance before for loop, and also lineOptions after for loop.

Hope this help.

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
  • ya it is working good thanks a lot but could you please explain the reason for this problem. i need to know why previously it was not working and now how it is working – dawood rizwan Feb 23 '15 at 12:50
0

Now Again Google has released a new API but they haven't updated the documentation properly.To connect to the API, you need to create an instance of the Google Play services API client.

 mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();

Once you have connected to Google Play services and the location services API, you can get the last known location of a user's device. When your app is connected to these you can use the fused location provider's getLastLocation() method to retrieve the device location. To do this you have to implements this interfaces in your Class/Activity

implements
    ConnectionCallbacks, OnConnectionFailedListener

Then you can found this implemented methods

@Override
    public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

Here is my complete code into this thread . Documentation is here

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87