我想在我的谷歌地图上添加路线

时间:2015-02-23 12:05:43

标签: android google-maps

我正在尝试在我的谷歌地图上显示路线...我正在开发一个出租车预订应用程序,其中我得到两个值"来自"和" To"来自用户,他们想去哪里?

但我正面临着在地图中添加路线的问题......当我提交价值时,我强行关闭......

这是我的编码信息......

快照镜头上的Dialogue.java类

public class Dialoge extends Activity{
    Button b;
    EditText e1,e2;

    String addressfrom,addressto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dia);
        e1=(EditText)findViewById(R.id.from);
        e2=(EditText)findViewById(R.id.To);

        b=(Button)findViewById(R.id.Submit);


        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v==b){
                    addressfrom = e1.getText().toString();
                    addressto = e2.getText().toString();
                    Intent i = new Intent(Dialoge.this,RideNow.class);
                    i.putExtra("from", addressfrom);
                    i.putExtra("to", addressto);
                    startActivity(i);

                    /*String format = "geo:0,0?q="+addressto;
                    Uri uri = Uri.parse(format); 
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);*/



            }
            }
        });


    }



}

我的RideNow.java作业显示路线并在按下提交按钮时被调用

public class RideNow extends Activity {
    GoogleMap mMap;
    String fromString,toString,url,polyline;
    double latitude,longitude,latitudeto,longitudeto;
    ProgressDialog pd;
    LatLng l,l2;
    double latt;
    List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>();
    List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
    List<LatLng> list = null;
    PolylineOptions p = null;
    ArrayList<LatLng> points = null;
    TextView t1,t2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ridenow);

        t1 = (TextView)findViewById(R.id.text1);
        t2 = (TextView)findViewById(R.id.text2);

        MapFragment m = (MapFragment)getFragmentManager().findFragmentById(R.id.fragment1);
        mMap = m.getMap();
        //mv.setBuiltInZoomControls(true);
        ParseAnalytics.trackAppOpened(getIntent());

        Intent i = getIntent();
        Bundle b = i.getExtras();
        fromString = b.getString("from");
        toString = b.getString("to");

        Geocoder geoCoder = new Geocoder(RideNow.this, Locale.getDefault());
        try {
            //########## Address From ###########
              List<Address> addressesfrom =
          geoCoder.getFromLocationName(fromString, 1); 
              if (addressesfrom.size() >  0) {
                 latitude = addressesfrom.get(0).getLatitude(); 
                 longitude =addressesfrom.get(0).getLongitude(); }

             //######### Address To ##########

              List<Address> addressesto =
                      geoCoder.getFromLocationName(toString, 1); 
                          if (addressesto.size() >  0) {
                             latitudeto = addressesto.get(0).getLatitude(); 
                             longitudeto =addressesto.get(0).getLongitude(); }

            l = new LatLng(latitude, longitude);
            l2 = new LatLng(latitudeto, longitudeto);
            url = getDirectionsUrl(l, l2);
            t1.setText(""+latitude+","+longitude);
            t2.setText(""+latitudeto+","+longitudeto);
            new json().execute();

            MarkerOptions marker = new MarkerOptions().position(l).title(""+fromString);

            mMap.addMarker(marker);
            MarkerOptions markerto = new MarkerOptions().position(new LatLng(latitudeto, longitudeto)).title(""+toString);
            CameraPosition cameraPositionto = new CameraPosition.Builder().target(new LatLng(latitudeto, longitudeto)).bearing(45).tilt(90).zoom(mMap.getCameraPosition().zoom).build();
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPositionto));
            mMap.addMarker(markerto);


            mMap.addPolyline(p);



          } catch (IOException e) { // TODO Auto-generated catch block
          e.printStackTrace(); }


    }
    private String getDirectionsUrl(LatLng origin,LatLng dest){

        // Origin of route
        String str_origin = "origin="+origin.latitude+","+origin.longitude;

        // Destination of route
        String str_dest = "destination="+dest.latitude+","+dest.longitude;

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = str_origin+"&"+str_dest+"&"+sensor;

        // Output format
        String output = "json";

        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;

        return url;
    }
    public class json extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if(jsonStr!=null){
                try{
                    JSONObject jobj = new JSONObject(jsonStr);
                    JSONArray jroute = jobj.getJSONArray("routes");


                        JSONObject j = jroute.getJSONObject(0);
                        JSONObject jbound = j.getJSONObject("bounds");
                        JSONObject jn = jbound.getJSONObject("northeast");
                        String d = jn.getString("lat");

                        JSONArray jlegs = j.getJSONArray("legs");
                        JSONObject jobjj = jlegs.getJSONObject(0);

                        JSONObject jduration =jobjj.getJSONObject("duration");


                        JSONArray steps = jobjj.getJSONArray("steps");
                        JSONObject jsteps = steps.getJSONObject(0);
                        JSONObject poly = jsteps.getJSONObject("polyline");
                        polyline = poly.getString("points");
                        list = decodePoly(polyline);
                        for(int l=0;l<list.size();l++){
                            HashMap<String, String> hm = new HashMap<String, String>();
                            hm.put("lat", Double.toString((list.get(l)).latitude) );
                            hm.put("lng", Double.toString((list.get(l)).longitude) );
                            path.add(hm);
                        }
                        points = new ArrayList<LatLng>();
                        for(int m=0;m<path.size();m++){
                            HashMap<String,String> point = path.get(m);

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

                            points.add(position);
                        }
                        //p.addAll(points);



                }catch(Exception e){}
                }
                return null;


        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(RideNow.this);
            pd.setTitle("Please Wait....");
            pd.setMessage("Wait");
            pd.setCancelable(false);
            pd.show();
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pd.dismiss();
            //Toast.makeText(getApplicationContext()," ("+polyline+")",Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), ""+latt, Toast.LENGTH_LONG).show();
            p.addAll(points);
        }

    }
    private List<LatLng> decodePoly(String encoded) {

        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
            poly.add(p);
        }

        return poly;
    }
}

我的Logcat

02-23 17:44:14.930: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:15.930: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:16.140: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:17.140: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:17.350: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:18.350: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:18.560: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:19.560: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:19.770: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:20.770: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:20.980: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:21.980: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:22.190: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:23.190: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:23.400: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:24.400: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:24.610: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:25.610: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:25.690: D/dalvikvm(950): GC_CONCURRENT freed 396K, 55% free 2671K/5831K, external 716K/1038K, paused 0ms+0ms
02-23 17:44:25.820: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:26.820: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:27.030: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:28.030: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:28.240: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:29.240: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:29.450: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:30.450: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:30.660: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:31.660: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:31.870: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:32.870: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:33.080: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:34.080: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:34.290: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:35.290: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:35.500: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:36.500: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:36.710: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:37.710: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:37.920: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:38.920: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:39.130: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:40.130: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:40.340: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:41.340: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:41.550: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:42.550: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:42.760: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:42.770: D/dalvikvm(2366): GC_CONCURRENT freed 384K, 54% free 2682K/5767K, external 716K/1038K, paused 0ms+0ms
02-23 17:44:43.760: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:43.970: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:44.970: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:45.180: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:46.180: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:46.390: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:47.390: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:47.600: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:48.600: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:48.810: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:49.810: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:50.020: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:51.020: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:51.230: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:52.230: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:52.440: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:53.440: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:53.650: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed
02-23 17:44:54.650: W/PGA(521): ctlSwapBuffers(133): Host Connect failed, err = -1(0xffffffff)
02-23 17:44:54.860: E/PGA(521): PgaSocketWriteAllHdipc: hd_ipc_send() failed

1 个答案:

答案 0 :(得分:0)

您的logcat显示这不是代码错误,而是PC上的模拟器错误。如果您在Bluestack Android Emulator上运行此代码,则可能会在logcat中出现此错误。

唯一可用的解决方案是退出Bluestacks(总是在后台运行),问题将解决!!