从一个位置移动在图形页面映射到另一个机器人 [英] moving map in mapview from one location to another in android

查看:323
本文介绍了从一个位置移动在图形页面映射到另一个机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的情况下我应该从一个位置移动自己的地图到另一个位置....我是很新的android..and我发现了如何画一条线在两个位置之间..

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

public class Mapview extends MapActivity implements LocationListener{
MapController mc;
GeoPoint p,p1,p2;
LocationManager lm;
String provider;
 class Mapoverlay extends com.google.android.maps.Overlay
 {

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
        Point screenpoints=new Point();     
        Point screenpoints1=new Point();
        Point screenpoints2=new Point();
         Paint paint;
         paint = new Paint();
         paint.setColor(Color.RED);
         paint.setAntiAlias(true);
         paint.setStyle(Style.FILL);
         paint.setStrokeWidth(2);
         paint.setAlpha(120);
         Paint paint1;
         paint1 = new Paint();
         paint1.setColor(Color.BLUE);
         paint1.setAntiAlias(true);
         paint1.setStyle(Style.FILL);
         paint1.setStrokeWidth(2);
         paint1.setAlpha(120);
        if(p != null)
        {
            mapView.getProjection().toPixels(p, screenpoints);
            mapView.getProjection().toPixels(p1, screenpoints1);
            mapView.getProjection().toPixels(p2, screenpoints2);
//              Bitmap map=BitmapFactory.decodeResource(getResources(),      R.drawable.pushpin1);
//               canvas.drawBitmap(map, screenpoints.x, screenpoints.y-53, null);
             canvas.drawLine(screenpoints.x,screenpoints.y,screenpoints1.x,screenpoints1.y, paint);
             canvas.drawLine(screenpoints.x,screenpoints.y,screenpoints2.x,screenpoints2.y, paint);
        }

        return true;

    }

 }

@Override
protected void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.mapview);
    MapView map=(MapView)findViewById(R.id.mapView);
    map.setBuiltInZoomControls(true);
     mc=map.getController();
     String[] coordinates={"51.4750766129036", "-3.15672342166522"};
     double lat = Double.parseDouble(coordinates[0]);
     double lng = Double.parseDouble(coordinates[1]);

     p=new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
     String[] coordinates1={"17.453117" , "78.467586" };
     double lat1 = Double.parseDouble(coordinates1[0]);
     double lng1 = Double.parseDouble(coordinates1[1]);

     p2=new GeoPoint((int)(lat1*1E6), (int)(lng1*1E6));
      p1 = new GeoPoint(19240000,-99120000);

//        p = new GeoPoint(19241000,-99121000);
     lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

     Criteria criteria = new Criteria();
     criteria.setAccuracy(Criteria.ACCURACY_FINE);
     criteria.setAltitudeRequired(false);
     criteria.setBearingRequired(false);
     criteria.setCostAllowed(true);
     criteria.setPowerRequirement(Criteria.POWER_LOW);
     provider=lm.getBestProvider(criteria, true);


     Location loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     List<String>  lists=lm.getAllProviders();
//       float f=loc.distanceTo(loc);
     if (loc!=null){
         System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(loc); 
     }else{
         Toast.makeText(getApplicationContext(), "Provider " + provider+ loc +  lists+" has not been selected.", 5000).show();
//           this.finish();
     }

//       Toast.makeText(getApplicationContext(), p.getLatitudeE6()+p.getLongitudeE6(), Toast.LENGTH_SHORT).show();
     mc.animateTo(p);
     mc.setZoom(1);
     map.invalidate();
     Mapoverlay mapOverlay = new Mapoverlay();
        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

}

我停留在从一个位置移动地图到另一个,而用户活动

i am stuck at moving map from one location to another without user activity

例如:当应用程序启动纽约市的位置,然后显示在地图应该搬到得克萨斯州...任何帮助预订购感激

for example:when the app initiates location of newyork city is shown then the map should move to texas ...any help willl be grateful

推荐答案

尝试这个类只是通过活动的行为,SRC的GeoPoint,DEST的GeoPoint,诠释色彩,图形页面mMapView

To call this class do this

    new getRoute().DrawPath(this,mGeoPoint,getGeoPoint,Color.RED,mMapView);  





  public class getRoute 
    {
        /*Variable*/
        double mIntentLatitude, mIntentLongitude;
        double mCursorLatitude, mCursorLongitude, mStartLatitude, mStartLongitude, mEndLatitude, MEndLongitude;
        String mJsonData;
        String mEnd_Address, mStart_Address, mName;
        int mLength;
        GeoPoint mGeoPoint = new GeoPoint(220328269, 725588202);
        double mLatitude_End[], mLongitude_End[], mLatitude_Start[], mLongitude_Start[];
        Activity activity;


        public getRoute
        (){

        }   
        boolean error = false;
        public void DrawPath(Activity act,GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
        {
            // connect to map web service
            DrawPathBack draw = new DrawPathBack(act, src, dest, color, mMapView01);
            draw.execute("Draw");
        }
        public class DrawPathBack extends AsyncTask<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;



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

            }
            @Override
            protected Void doInBackground(String... params)
            {



                HttpParams httpParameters = new BasicHttpParams();

                HttpConnectionParams.setSoTimeout(httpParameters, 60000);

                HttpClient client = new DefaultHttpClient();

                try {

                    HttpPost httpPost = new HttpPost("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));
                    HttpResponse httpResponse = 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();
                }
                return null;
            }
            @Override
            protected void onProgressUpdate(Integer... progress)
            {
                bar.setMessage("Time Progress "+progress[0]);
            }
            @Override
            protected void onPostExecute(Void result) 
            {
                try {

                    JSONObject jsonObj = new JSONObject(mJsonData);

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

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

                        JSONObject rout = routes.getJSONObject(i);

                        JSONObject bounds = rout.getJSONObject("bounds");

                        JSONObject northest = bounds.getJSONObject("northeast");

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

                        JSONObject southwest = 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 legs
                        JSONArray legs = rout.getJSONArray("legs");
                        System.out.println("length of legs array     " + legs.length());
                        for (int j = 0; j < legs.length(); j++) {
                            JSONObject leg = legs.getJSONObject(j);
                            System.out.println("enter in second array");

                            JSONObject distance = leg.getJSONObject("distance");
                            String mTextDistent = distance.getString("text");
                            System.out.println("distances bitween two point" + mTextDistent);
                            JSONObject duration = leg.getJSONObject("duration");
                            String mTextDurestion = 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);

                            JSONArray step = leg.getJSONArray("steps");

                            mLength = step.length();

                            mLatitude_End = new double[mLength];
                            mLongitude_End = new double[mLength];
                            mLatitude_Start = new double[mLength];
                            mLongitude_Start = new double[mLength];

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

                                JSONObject st = step.getJSONObject(k);

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

                                JSONObject start_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 (int mDistanse = 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();
                }
            }
        }

        public class MapOverlay extends com.google.android.maps.Overlay {

            public MapOverlay(GeoPoint mGeoPoint) {

            }

            @Override
            public boolean draw(final Canvas canvas, MapView mapView,
                    boolean shadow, long when) {

                Point mpoint = new Point();
                Point mpoint1 = new Point();

                int let[] = new int[mLength];
                int lon[] = new int[mLength];
                int let_end[] = new int[mLength];
                int lon_end[] = new int[mLength];
                GeoPoint newGeoPoint = null;
                GeoPoint newGeoPoint1;
                for (int k = 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 = new GeoPoint(let_end[k], lon_end[k]);
                    newGeoPoint = new GeoPoint(let[k], lon[k]);
                    mapView.getProjection().toPixels(newGeoPoint, mpoint);
                    mapView.getProjection().toPixels(newGeoPoint1, mpoint1);
                    Paint paint = new Paint();

                    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){
                        Bitmap bmp = BitmapFactory.decodeResource(activity.getResources(), R.drawable.green_pin);
                        canvas.drawBitmap(bmp,mpoint1.x ,mpoint1.y-30, null);
                    }
                    GeoPoint dumy=new GeoPoint((int)(mIntentLatitude * 1E6), (int)(mIntentLongitude * 1E6));
                    Point dumy1 = new Point();
                    mapView.getProjection().toPixels(dumy, dumy1);
                    Bitmap bmp = BitmapFactory.decodeResource(activity.getResources(), R.drawable.dest_pin);
                    canvas.drawBitmap(bmp,dumy1.x,dumy1.y - 25, null);
                    mapView.getController().animateTo(dumy);
                    mapView.invalidate();
                }
                return false;

            }

        }
    }

这篇关于从一个位置移动在图形页面映射到另一个机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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