0

現在地と地図上の複数のポイントを表示するために、独自のカスタムマップを作成しました。正常に動作しますが、現在の場所から地図上のすべてのポイントまでのルートを表示したいと思います。その方法についていくつか提案をいただけますか?どうも。

これは私のcustomMapアクティビティです:

public class CustomMapActivity extends MapActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

         String   provider = ""; 
         Criteria crit = new Criteria();
         Location loc = new Location("");
         LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

         MapView mapView = (MapView) findViewById(R.id.mapview);
         mapView.setBuiltInZoomControls(true);

         List<Overlay> mapOverlays = mapView.getOverlays();
         Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
         ItemizedOverlayedClass itemizedoverlay = new ItemizedOverlayedClass(drawable, this);

            try
            { 
             MyLocationOverlay mylocationOverlay = new MyLocationOverlay(this, mapView);
             mylocationOverlay.enableMyLocation();
             mapView.getOverlays().add(mylocationOverlay);          
             provider = mlocManager.getBestProvider(crit, false);
             loc = mlocManager.getLastKnownLocation(provider);
            }
            catch(Exception ex)
            {
            //Toast.makeText(this, "Unable to retrive provider", Toast.LENGTH_LONG).show();
            }


        for (ServiceActivity activity : DataSources.ActivitiesList)
        { 
            try
            {
                Location siteLocation =  new Location("");
                siteLocation.setLatitude(activity.SiteLatitude);
                siteLocation.setLongitude(activity.SiteLongitude);

                GeoPoint point = new GeoPoint((int)(activity.SiteLatitude * 1e6),(int)(activity.SiteLongitude * 1e6));
                OverlayItem overlayitem = new OverlayItem(point, activity.SiteName+ " " + activity.SiteAddress, "Distance to this location: " + String.valueOf(loc.distanceTo(siteLocation)/1000) + " km");

                itemizedoverlay.addOverlay(overlayitem);
                mapOverlays.add(itemizedoverlay);
            }
            catch(Exception ex)
            {
                @SuppressWarnings("unused")
                AlertDialogClass alert =  new AlertDialogClass(this,ex.getMessage());
            }
        }   




    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //mylocationOverlay.enableMyLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_map, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }


}

これはオーバーレイクラスです:

package com.example.srwebservice;


import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ItemizedOverlayedClass extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

    public ItemizedOverlayedClass(Drawable defaultMarker) 
    {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public ItemizedOverlayedClass(Drawable defaultMarker, Context context) 
    {
      super(boundCenterBottom(defaultMarker));
      mContext = context;
    }

    @Override
    protected OverlayItem createItem(int i) 
    {
        // TODO Auto-generated method stub
         return mOverlays.get(i);
    }

    @Override
    public int size()
    {
      return mOverlays.size();
    }

    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();
    }
    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }
}
4

3 に答える 3

2

このリンクには、アプリの地図上にルートパスを描画するためのチュートリアルがあります。これはあなたを助け、地図アプリとリンクするために以下を使用するかもしれません...これはグーグルマップアプリケーションであなたに連れて行きます

String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+source_latitude+","+source_longitude+"&daddr="+destination_latitude+","+destination_longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

また

このリンクを参照して、運転ルートを表示する方法

于 2012-11-26T10:41:58.907 に答える
2

もっと簡単な解決策を見つけました。Google マップのインテントを直接使用でき、カスタム マップ アクティビティを作成する必要はもうありません。

if(DataSources.ActivitiesList.length >0)
{
  String address = "http://maps.google.com/maps?daddr=" +    DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i <  DataSources.ActivitiesList.length ; i++) 
{
    if(DataSources.ActivitiesList[i].SiteLatitude != null)
        address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
    startActivity(intent);
    break;      
}
于 2012-11-26T13:16:38.237 に答える
0
package org.ci.geo.route.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.ci.geo.route.Road;
import org.ci.geo.route.RoadProvider;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapRouteActivity extends MapActivity {

        LinearLayout linearLayout;
        MapView mapView;
        private Road mRoad;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                mapView = (MapView) findViewById(R.id.mapview);
                mapView.setBuiltInZoomControls(true);

                new Thread() {
                        @Override
                        public void run() {
                                double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
                                String url = RoadProvider
                                                .getUrl(fromLat, fromLon, toLat, toLon);
                                InputStream is = getConnection(url);
                                mRoad = RoadProvider.getRoute(is);
                                mHandler.sendEmptyMessage(0);
                        }
                }.start();
        }

        Handler mHandler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                        TextView textView = (TextView) findViewById(R.id.description);
                        textView.setText(mRoad.mName + " " + mRoad.mDescription);
                        MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
                        List<Overlay> listOfOverlays = mapView.getOverlays();
                        listOfOverlays.clear();
                        listOfOverlays.add(mapOverlay);
                        mapView.invalidate();
                };
        };

        private InputStream getConnection(String url) {
                InputStream is = null;
                try {
                        URLConnection conn = new URL(url).openConnection();
                        is = conn.getInputStream();
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return is;
        }

        @Override
        protected boolean isRouteDisplayed() {
                return false;
        }
}

class MapOverlay extends com.google.android.maps.Overlay {
        Road mRoad;
        ArrayList<GeoPoint> mPoints;

        public MapOverlay(Road road, MapView mv) {
                mRoad = road;
                if (road.mRoute.length > 0) {
                        mPoints = new ArrayList<GeoPoint>();
                        for (int i = 0; i < road.mRoute.length; i++) {
                                mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
                                                (int) (road.mRoute[i][0] * 1000000)));
                        }
                        int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                                        mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                                        .getLatitudeE6()) / 2);
                        int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
                                        mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
                                        .getLongitudeE6()) / 2);
                        GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

                        MapController mapController = mv.getController();
                        mapController.animateTo(moveTo);
                        mapController.setZoom(7);
                }
        }

        @Override
        public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
                super.draw(canvas, mv, shadow);
                drawPath(mv, canvas);
                return true;
        }

        public void drawPath(MapView mv, Canvas canvas) {
                int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
                Paint paint = new Paint();
                paint.setColor(Color.GREEN);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(3);
                for (int i = 0; i < mPoints.size(); i++) {
                        Point point = new Point();
                        mv.getProjection().toPixels(mPoints.get(i), point);
                        x2 = point.x;
                        y2 = point.y;
                        if (i > 0) {
                                canvas.drawLine(x1, y1, x2, y2, paint);
                        }
                        x1 = x2;
                        y1 = y2;
                }
        }
}


http://code.google.com/p/j2memaprouteprovider/source/browse/trunk/J2MEMapRouteAndroidEx/src/org/ci/geo/route/android/MapRouteActivity.java
于 2012-11-26T10:44:26.250 に答える