Skip to content Skip to sidebar Skip to footer

Moving Map In Mapview From One Location To Another In Android

i have a situation in my app where i should move my map from one location to another location....i am quite new to android..and i found out how to draw a line in between two locat

Solution 1:

Try This class just pass Activity act,GeoPoint src,GeoPoint dest, int color, MapView mMapView

To call thisclassdothisnewgetRoute().DrawPath(this,mGeoPoint,getGeoPoint,Color.RED,mMapView);  





  publicclassgetRoute 
    {
        /*Variable*/double mIntentLatitude, mIntentLongitude;
        double mCursorLatitude, mCursorLongitude, mStartLatitude, mStartLongitude, mEndLatitude, MEndLongitude;
        String mJsonData;
        String mEnd_Address, mStart_Address, mName;
        int mLength;
        GeoPointmGeoPoint=newGeoPoint(220328269, 725588202);
        double mLatitude_End[], mLongitude_End[], mLatitude_Start[], mLongitude_Start[];
        Activity activity;


        publicgetRoute(){

        }   
        booleanerror=false;
        publicvoidDrawPath(Activity act,GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
        {
            // connect to map web serviceDrawPathBackdraw=newDrawPathBack(act, src, dest, color, mMapView01);
            draw.execute("Draw");
        }
        publicclassDrawPathBackextendsAsyncTask<String, Integer,Void>
        {
            ProgressDialog bar;
            List<Overlay> mListOverlay;
            MapOverlay mapOverlay;
            ItemizedOverlay<OverlayItem> mItemizedOverlay;

            GeoPoint src,dest;
            int color;
            MapView mMapView;
            CommonMethod mCommonMethod;
            String [] pairs;
            String[] lngLat;



            publicDrawPathBack(Activity act,GeoPoint gpsrc,GeoPoint gpdest, int c, MapView mMap)
            {
                activity =act;
                src=gpsrc;
                dest=gpdest;
                color = c;
                mMapView=mMap;
                mListOverlay = mMapView.getOverlays();
                mapOverlay = newMapOverlay(mGeoPoint);
                mListOverlay.add(mapOverlay);
                mMapView.invalidate();

            }
            @Overrideprotected Void doInBackground(String... params)
            {



                HttpParamshttpParameters=newBasicHttpParams();

                HttpConnectionParams.setSoTimeout(httpParameters, 60000);

                HttpClientclient=newDefaultHttpClient();

                try {

                    HttpPosthttpPost=newHttpPost("http://maps.googleapis.com/maps/api/directions/json?origin="+ (Double.toString((double)src.getLatitudeE6()/1.0E6)) + "," +(Double.toString((double)src.getLongitudeE6()/1.0E6 ) + "&destination=" + (Double.toString((double)dest.getLatitudeE6()/1.0E6)) + ","+  (Double.toString((double)dest.getLongitudeE6()/1.0E6))+ "&sensor=false"));
                    mIntentLatitude=((double)src.getLatitudeE6()/1.0E6);
                    mIntentLongitude=((double)src.getLongitudeE6()/1.0E6);

                    //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));HttpResponsehttpResponse= client.execute(httpPost);
                    mJsonData = EntityUtils.toString(httpResponse.getEntity());

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                returnnull;
            }
            @OverrideprotectedvoidonProgressUpdate(Integer... progress)
            {
                bar.setMessage("Time Progress "+progress[0]);
            }
            @OverrideprotectedvoidonPostExecute(Void result) 
            {
                try {

                    JSONObjectjsonObj=newJSONObject(mJsonData);

                    // grabbing the routes objectJSONArrayroutes= jsonObj.getJSONArray("routes");

                    for (inti=0; i < routes.length(); i++) {

                        JSONObjectrout= routes.getJSONObject(i);

                        JSONObjectbounds= rout.getJSONObject("bounds");

                        JSONObjectnorthest= bounds.getJSONObject("northeast");

                        mStartLatitude = northest.getDouble("lat");
                        mStartLongitude = northest.getDouble("lng");

                        JSONObjectsouthwest= bounds.getJSONObject("southwest");
                        mEndLatitude = southwest.getDouble("lat");
                        MEndLongitude = southwest.getDouble("lng");
                        System.out.println("get data from jeson" + mCursorLatitude + mCursorLongitude + mEndLatitude
                                + MEndLongitude);

                        // grabbing the routes legsJSONArraylegs= rout.getJSONArray("legs");
                        System.out.println("length of legs array     " + legs.length());
                        for (intj=0; j < legs.length(); j++) {
                            JSONObjectleg= legs.getJSONObject(j);
                            System.out.println("enter in second array");

                            JSONObjectdistance= leg.getJSONObject("distance");
                            StringmTextDistent= distance.getString("text");
                            System.out.println("distances bitween two point" + mTextDistent);
                            JSONObjectduration= leg.getJSONObject("duration");
                            StringmTextDurestion= duration.getString("text");

                            mEnd_Address = leg.getString("end_address");

                            mStart_Address = leg.getString("start_address");
                            System.out.println("get data from jeson in second arry"
                                    + mTextDurestion + " " + mEnd_Address + "  " + mStart_Address);

                            JSONArraystep= leg.getJSONArray("steps");

                            mLength = step.length();

                            mLatitude_End = newdouble[mLength];
                            mLongitude_End = newdouble[mLength];
                            mLatitude_Start = newdouble[mLength];
                            mLongitude_Start = newdouble[mLength];

                            for (intk=0; k < step.length(); k++) {

                                JSONObjectst= step.getJSONObject(k);

                                System.out.println("enter in third array");
                                JSONObjectend_lo= st.getJSONObject("end_location");

                                JSONObjectstart_lo= st
                                .getJSONObject("start_location");

                                mLatitude_End[k] = end_lo.getDouble("lat");
                                mLongitude_End[k] = end_lo.getDouble("lng");

                                mLatitude_Start[k] = start_lo.getDouble("lat");
                                mLongitude_Start[k] = start_lo.getDouble("lng");

                            }
                            for (intmDistanse=0; mDistanse < mLength; mDistanse++) {
                                System.out.println("end location let" + mLatitude_End[mDistanse]);
                                System.out.println("end location long+"
                                        + mLongitude_End[mDistanse]);
                                System.out.println("Start location let"
                                        + mLatitude_Start[mDistanse]);
                                System.out.println("Start location long"
                                        + mLongitude_Start[mDistanse]);
                            }
                        }
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }

        publicclassMapOverlayextendscom.google.android.maps.Overlay {

            publicMapOverlay(GeoPoint mGeoPoint) {

            }

            @Overridepublicbooleandraw(final Canvas canvas, MapView mapView,
                    boolean shadow, long when) {

                Pointmpoint=newPoint();
                Pointmpoint1=newPoint();

                int let[] = newint[mLength];
                int lon[] = newint[mLength];
                int let_end[] = newint[mLength];
                int lon_end[] = newint[mLength];
                GeoPointnewGeoPoint=null;
                GeoPoint newGeoPoint1;
                for (intk=0; k < mLength; k++) {

                    let[k] = (int) (mLatitude_Start[k] * 1E6);
                    lon[k] = (int) (mLongitude_Start[k] * 1E6);

                    let_end[k] = (int) (mLatitude_End[k] * 1E6);
                    lon_end[k] = (int) (mLongitude_End[k] * 1E6);
                    newGeoPoint1 = newGeoPoint(let_end[k], lon_end[k]);
                    newGeoPoint = newGeoPoint(let[k], lon[k]);
                    mapView.getProjection().toPixels(newGeoPoint, mpoint);
                    mapView.getProjection().toPixels(newGeoPoint1, mpoint1);
                    Paintpaint=newPaint();

                    paint.setColor(Color.RED);
                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    paint.setStrokeWidth(5);
                    canvas.drawCircle(mpoint.x, mpoint.y, 5, paint);
                    paint.setColor(Color.BLUE);
                    canvas.drawLine(mpoint.x, mpoint.y, mpoint1.x, mpoint1.y, paint);
                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(100);
                    paint.setStyle(Paint.Style.FILL);
                    if(k==mLength-1){
                        Bitmapbmp= BitmapFactory.decodeResource(activity.getResources(), R.drawable.green_pin);
                        canvas.drawBitmap(bmp,mpoint1.x ,mpoint1.y-30, null);
                    }
                    GeoPoint dumy=newGeoPoint((int)(mIntentLatitude * 1E6), (int)(mIntentLongitude * 1E6));
                    Pointdumy1=newPoint();
                    mapView.getProjection().toPixels(dumy, dumy1);
                    Bitmapbmp= BitmapFactory.decodeResource(activity.getResources(), R.drawable.dest_pin);
                    canvas.drawBitmap(bmp,dumy1.x,dumy1.y - 25, null);
                    mapView.getController().animateTo(dumy);
                    mapView.invalidate();
                }
                returnfalse;

            }

        }
    }

Solution 2:

Try this if you want to move map on particular position then get lat or long of that position and just pass in this :

MapController mMapController;

mMapController=mMapView.getController();
    mMapController.setCenter(newGeoPoint((int)(lat * 1E6), (int)(lon * 1E6)));

and if you want to draw path between two location then just pass lat and lon in this :

Intent intent = newIntent(android.content.Intent.ACTION_VIEW, 
                Uri.parse("http://maps.google.com/maps?saddr="+ String.valueOf(from_lattitude) +","+ String.valueOf(from_longitude) +"&daddr="+ String.valueOf(dest_lati) +","+ String.valueOf(dest_longi)));
                startActivity(intent);

Post a Comment for "Moving Map In Mapview From One Location To Another In Android"