谷歌地图折线是不工作的的Andr​​iod 5.0版本(棒棒糖) [英] google map polyline is not working on andriod version 5.0(lollipop)

查看:268
本文介绍了谷歌地图折线是不工作的的Andr​​iod 5.0版本(棒棒糖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制折线GoogleMap的,它高达Android版本4.4(KitKat)的工作正常,但它不是在的是Android 5.0(棒棒糖)工作。如何在工作棒棒糖也做。你可以请帮我锄解决这个问题,这是我的谷歌地图code

 公共类MapLocation扩展FragmentActivity实现OnMarkerClickListener {    GoogleMap的地图;
    ArrayList的<&经纬度GT; markerPoints;    原产地的LatLng,dest1,dest2,dest3;
    标记MARKER1,MARKER2,marker3;    的PolylineOptions lineOptions =新的PolylineOptions();    折线线;
    INT POS;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.gmap_direction);        markerPoints =新的ArrayList<&经纬度GT;();        //获取参考activity_main的SupportMapFragment
        SupportMapFragment FM =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。        //获取地图的SupportMapFragment
        地图= fm.getMap();        如果(图!= NULL){            //在地图启用MyLocation按钮
            map.setMyLocationEnabled(真);            //从系统服务LOCATION_SERVICE获取的LocationManager对象
            的LocationManager的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);            //创建一个标准的对象来检索提供商
            标准标准=新标准();            //获得最佳供应商的名称
            字符串提供商= locationManager.getBestProvider(标准,真正的);            //获取当前位置
            地点= locationManager.getLastKnownLocation(供应商);
            POS = getIntent()getIntExtra(POS,0);
            尝试{
            开关(POS){               情况1:                   dest2 =新的经纬度(18.545981,73.902132);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"PUNE,KALYANINAGAR\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
               案例2:                   dest2 =新的经纬度(17.429122,78.412745);
                   MARKER2 ​​= map.addMarker(。新的MarkerOptions()位置(dest2).title伪(Vivero国际preschool)片段(银禧山庄)图标(BitmapDesc​​riptorFactory.defaultMarker(BitmapDesc​​riptorFactory.HUE_RED)));
                   打破;
               案例3:                   dest2 =新的经纬度(19.115177,72.909486);
                   MARKER2 ​​= map.addMarker(。新的MarkerOptions()位置(dest2).title伪(Vivero国际preschool)片段(孟买博维)图标(BitmapDesc​​riptorFactory.defaultMarker(BitmapDesc​​riptorFactory.HUE_RED)));
                   打破;
               情况4:                   dest2 =新的经纬度(18.545981,73.902132);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"PUNE,MAGARPATTA\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
               情况5:                   dest2 =新的经纬度(12.924438,77.631537);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"Koramangala\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
               情况6:                   dest2 =新的经纬度(12.9044019,77.6690773);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"Sarjapur\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
               案例7:                   dest2 =新的经纬度(12.972442,77.639195);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"Indiranagar\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
               案例8:                   dest2 =新的经纬度(12.968719,77.747883);
                   MARKER2 ​​= map.addMarker(新的MarkerOptions()位置(dest2).title伪(Vivero国际$p$pschool\").snippet(\"Whitefield\").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                   打破;
                   // dest1 =新的经纬度(19.115177,72.909486);
            }            //在谷歌地图显示的当前位置
            map.moveCamera(CameraUpdateFactory.newLatLng(dest2));            //放大的谷歌地图
            map.animateCamera(CameraUpdateFactory.zoomTo(15));            marker2.showInfoWindow();
            如果(位置!= NULL){
                onLocationChanged(位置);            字符串URL = getDirectionsUrl(产地,dest2);            DownloadTask downloadTask =新DownloadTask();            //开始从谷歌地图API下载JSON数据
            downloadTask.execute(URL);
            }
            }
            赶上(例外五){            }
        }
    }
    / **一个类来分析JSON格式*的地方谷歌/
    私有类ParserTask扩展的AsyncTask<字符串,整数,列表<名单,LT; HashMap的<字符串,字符串>>> > {        //解析在非UI线程数据
        @覆盖
        保护名单,LT;名单<&HashMap的LT;字符串,字符串>>> doInBackground(字符串... jsonData){            的JSONObject jObject;
            清单<名单,LT; HashMap的<字符串,字符串>>>路线= NULL;            尝试{
                jObject =新的JSONObject(jsonData [0]);
                DirectionsJSONParser分析器=新DirectionsJSONParser();                //开始分析数据
                路线= parser.parse(jObject);
            }赶上(例外五){
                e.printStackTrace();
            }
            返回路线;
        }        //执行的UI线程,解析过程后,
        @覆盖
        保护无效onPostExecute(列表<名单,LT; HashMap的<字符串,字符串>>>的结果){
            ArrayList的<&经纬度GT;点= NULL;
            //的PolylineOptions lineOptions = NULL;            的MarkerOptions的MarkerOptions =新的MarkerOptions();            //通过所有的穿越路线
            的for(int i = 0; I< result.size();我++){
                点=新的ArrayList<&经纬度GT;();
                // lineOptions =新的PolylineOptions();                //抓取第i个路线
                清单<&HashMap的LT;字符串,字符串>>路径= result.get(ⅰ);                //取回所有的点在第i个路线
                对于(INT J = 0; J< path.size(); J ++){
                    HashMap的<字符串,字符串>点= path.get(J);                    双纬度= Double.parseDouble(point.get(纬度));
                    双LNG = Double.parseDouble(point.get(LNG));
                    位置的LatLng =新经纬度(纬度,经度);                    points.add(位置);
                }                //在路由添加的所有点LineOptions
                lineOptions.addAll(点);
                lineOptions.width(4);
                lineOptions.color(Color.BLUE);            }            //绘制折线在谷歌地图为第i个路线
            //map.addPolyline(lineOptions);
            行= map.addPolyline(lineOptions);
        }
    }
}


解决方案

尝试修改codeS为你的 onPostExecute

  @覆盖
保护无效onPostExecute(列表<名单,LT; HashMap的<字符串,字符串>>>的结果){
        ArrayList的<&经纬度GT;点= NULL;
        //的PolylineOptions lineOptions = NULL;        //的MarkerOptions的MarkerOptions =新的MarkerOptions();        点=新的ArrayList<&经纬度GT;();        //通过所有的穿越路线
        的for(int i = 0; I< result.size();我++){
            //点​​=新的ArrayList<&经纬度GT;();
            // lineOptions =新的PolylineOptions();            //抓取第i个路线
            清单<&HashMap的LT;字符串,字符串>>路径= result.get(ⅰ);            //取回所有的点在第i个路线
            对于(INT J = 0; J< path.size(); J ++){
                HashMap的<字符串,字符串>点= path.get(J);                双纬度= Double.parseDouble(point.get(纬度));
                双LNG = Double.parseDouble(point.get(LNG));
                位置的LatLng =新经纬度(纬度,经度);                points.add(位置);
            }            //在路由添加的所有点LineOptions
            //lineOptions.addAll(points);
            //lineOptions.width(4);
            //lineOptions.color(Color.BLUE);        }        //绘制折线在谷歌地图为第i个路线
        //map.addPolyline(lineOptions);        lineOptions.addAll(点);
        lineOptions.width(4);
        lineOptions.color(Color.BLUE);
        行= map.addPolyline(lineOptions);
    }

我搬到你的清单实例之前,循环,也 lineOptions 循环。

希望这有助于。

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);
        }           
    }
} 

解决方案

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.

这篇关于谷歌地图折线是不工作的的Andr​​iod 5.0版本(棒棒糖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆