Bing Maps Mobile Sdk路线映射从A点到B点

时间:2018-09-12 10:01:10

标签: android maps bing-maps

我正在绘制从A点到B点的路线。使用Bing Sdk。在虚拟项目中,它工作正常。但是当我将代码与我的项目集成在一起时。没用 问题: 它不在处理程序内部,该处理程序将从RouteAsync线程接收数据。我将在此处发布我的两个班级的代码。任何帮助将不胜感激。

public class ViewLoadDetailsFragment extends Fragment {

@SuppressWarnings("unused")
private final MapMovedListener mapMovedListener = new MapMovedListener() {
    public void onAvailableChecked() {
        // OPTION Add logic to Update Layers here.
        // This will update data layers when the map is moved.
    }
};
CharSequence[] _dataLayers;
boolean[] _dataLayerSelections;
private BingMapsView bingMapsView;
private GPSManager _GPSManager;
private EntityLayer _gpsLayer;
private ProgressDialog _loadingScreen;
private boolean isMapLoaded = false;

/**
 * Handler for loading Screen
 */
protected Handler loadingScreenHandler = new Handler() {
    public void handleMessage(Message msg) {
        if (msg.arg1 == 0) {
            _loadingScreen.hide();
        } else {
            _loadingScreen.show();
        }
    }
};
private Activity _baseActivity;
private ImageButton btnLayers;
private ImageButton btnZoomIn;
private ImageButton btnZoomOut;
private ImageButton btnMyLocation;

public static boolean checkPermission(final Context context) {
    return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.view_load_details, container, false);
    _baseActivity = getActivity();
    // Load the map
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            throwable.printStackTrace();
        }
    });

    btnZoomIn = (ImageButton) view.findViewById(R.id.btnZoomIn);
    btnZoomIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bingMapsView.zoomIn();
        }
    });

    btnZoomOut = (ImageButton) view.findViewById(R.id.btnZoomOut);
    btnZoomOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bingMapsView.zoomOut();
        }
    });

    btnMyLocation = (ImageButton) view.findViewById(R.id.btnMyLocation);
    btnMyLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           /* Coordinate coord = _GPSManager.GetCoordinate();
            if (coord != null) {
                // Center on users GPS location
               *//* bingMapsView.setCenterAndZoom(coord,
                        Constants.DefaultGPSZoomLevel);*//*
            }*/

            //This Code Will Execute on Location Button Click
            DailogLauncher.LaunchDirectionsDialog(_baseActivity, bingMapsView,
                    loadingScreenHandler);
            Toast.makeText(getActivity(),"Method Launched",Toast.LENGTH_SHORT).show();
            Log.d("CheckResponse", "onClick: " + "Method Executed");

        }
    });

    Initialize(view);

   /* while (isMapLoaded){
        DailogLauncher.LaunchDirectionsDialog(MainActivity.this, bingMapsView,
                loadingScreenHandler);
        Toast.makeText(MainActivity.this,"Method Launched",Toast.LENGTH_SHORT).show();
    }*/


    return view;
}

private void Initialize(View view) {
    _baseActivity = getActivity();

    if (!checkPermission(getContext())) {
        ActivityCompat.requestPermissions(
                getActivity(),
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_LOCATION_REQUEST_CODE);
    }

    _GPSManager = new GPSManager(getActivity(), new GPSLocationListener());

    // Add more data layers here
    _dataLayers = new String[]{"Traffic"};
    _dataLayerSelections = new boolean[_dataLayers.length];

    _loadingScreen = new ProgressDialog(getContext());
    _loadingScreen.setCancelable(false);
    _loadingScreen.setMessage("Loading...");

    bingMapsView = (BingMapsView) view.findViewById(R.id.mapView);

   /* // Create handler to switch out of Splash screen mode
    final Handler viewHandler = new Handler() {
        public void handleMessage(Message msg) {
            ((ViewFlipper) findViewById(R.id.flipper)).setDisplayedChild(1);
        }
    };*/

    // Add a map loaded event handler
    bingMapsView.setMapLoadedListener(new MapLoadedListener() {
        public void onAvailableChecked() {
           /* // hide splash screen and go to map
            viewHandler.sendEmptyMessage(0);*/

            // Add GPS layer
            _gpsLayer = new EntityLayer(Constants.DataLayers.GPS);
            bingMapsView.getLayerManager().addLayer(_gpsLayer);
            UpdateGPSPin();
            updateMarker();
            Toast.makeText(getActivity(),"Map Loaded",Toast.LENGTH_SHORT).show();
            //mapLoadListner.maploaded();
           /* DailogLauncher.LaunchDirectionsDialog(getActivity(), bingMapsView,
                    loadingScreenHandler);
            Toast.makeText(getActivity(),"Method Launched",Toast.LENGTH_SHORT).show();*/

        }
    });

    /*// Add a entity clicked event handler
    bingMapsView.setEntityClickedListener(new EntityClickedListener() {
        public void onAvailableChecked(String layerName, int entityId) {
            HashMap<String, Object> metadata = bingMapsView
                    .getLayerManager().GetMetadataByID(layerName, entityId);
            DialogLauncher.LaunchEntityDetailsDialog(_baseActivity,
                    metadata);
        }
    });*/

    // Load the map
    bingMapsView.loadMap(Constants.BingMapsKey,
            _GPSManager.GetCoordinate(), Constants.DefaultGPSZoomLevel);
}


private void UpdateGPSPin() {
    PushpinOptions opt = new PushpinOptions();
    opt.Icon = Constants.PushpinIcons.GPS;
    Coordinate coordinate = new Coordinate(33.738045,73.084488);
    Pushpin p = new Pushpin(coordinate, opt);
    if (p.Location != null && _gpsLayer != null) {
        _gpsLayer.clear();
        _gpsLayer.add(p);
        _gpsLayer.updateLayer();
    }
}

public void updateMarker() {
    List<Coordinate> listCoord = new ArrayList<>();
    // EntityLayer is used for map overlay
    EntityLayer entityLayer = (EntityLayer) bingMapsView.getLayerManager()
            .getLayerByName(Constants.DataLayers.Search);
    if (entityLayer == null) {
        entityLayer = new EntityLayer(Constants.DataLayers.Search);
    }
    entityLayer.clear();
    // Use Pushpin to mark on the map
    // PushpinOptions is used to set attributes for Pushpin
    // opt.Icon - The icon of PushPin, opt.Anchor - The position to display Pushpin
    PushpinOptions opt = new PushpinOptions();
    opt.Icon = Constants.PushpinIcons.RedFlag;
    opt.Width = 20;
    opt.Height = 35;
    opt.Anchor = new Point(11, 10);

    // Add the entityLayer to mapView's LayerManager
    bingMapsView.getLayerManager().addLayer(entityLayer);
    entityLayer.updateLayer();

    // set the center location and zoom level of map
    Coordinate coordinate = new Coordinate(33.738045,73.084488);
    bingMapsView.setCenterAndZoom(coordinate, 11);

    // Polyline used to draw lines on the MapView
    // PolylineOptions have multiple attributes for the line
    // polylineOptions.StrokeThickness
    // polylineOptions.StrokeColor
    Polyline routeLine = new Polyline(listCoord);
    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.StrokeThickness = 3;
    routeLine.Options = polylineOptions;
    entityLayer.add(routeLine);
}


public class GPSLocationListener implements LocationListener {
    public void onLocationChanged(Location arg0) {
        UpdateGPSPin();
    }

    public void onProviderDisabled(String arg0) {
    }

    public void onProviderEnabled(String arg0) {
    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

}

这里是我的DailogLauncher类的代码。

public class DailogLauncher {

public static void LaunchDirectionsDialog(Activity activity, final BingMapsView bingMapsView, final Handler loadingScreenHandler) {

    String fromText = "islamabad";
    String toText = "lahore";

    if (!Utilities.isNullOrEmpty(fromText) && !Utilities.isNullOrEmpty(toText)) {
        Message viewMsg = new Message();
        viewMsg.arg1 = 1;
        loadingScreenHandler.sendMessage(viewMsg);
        Log.d("CheckResponse", "Dailog Launcher " + "Before Try Block");
        try {
            BingMapsRestService bmService = new BingMapsRestService(Constants.BingMapsKey);
            bmService.RouteAsyncCompleted = new Handler() {
                public void handleMessage(Message msg) {
                    if (msg.obj != null) {
                        org.bingmaps.rest.models.Route route = (org.bingmaps.rest.models.Route) msg.obj;
                        Log.d("CheckResponse", "Dailog Launcher " + "In Handler");

                        EntityLayer routeLayer = (EntityLayer) bingMapsView.getLayerManager().getLayerByName(Constants.DataLayers.Route);
                        Log.d("CheckResponse", "Dailog Launcher " + "In Handler 2");

                        if (routeLayer == null) {
                            routeLayer = new EntityLayer(Constants.DataLayers.Route);
                        }

                        routeLayer.clear();

                        PushpinOptions pOption1 = new PushpinOptions();
                        pOption1.Icon = Constants.PushpinIcons.Start;
                        pOption1.Width = 33;
                        pOption1.Height = 43;
                        pOption1.Anchor = new Point(4, 40);
                        Log.d("CheckResponse", "Dailog Launcher " + "In Handler 3");

                        Pushpin start = new Pushpin(route.RouteLegs.get(0).ActualStart, pOption1);
                        routeLayer.add(start);

                        PushpinOptions pOption2 = pOption1.clone();
                        pOption2.Icon = Constants.PushpinIcons.End;

                        Pushpin end = new Pushpin(route.RouteLegs.get(0).ActualEnd, pOption2);
                        routeLayer.add(end);

                        Polyline routeLine = new Polyline(route.RoutePath);
                        routeLayer.add(routeLine);

                        Log.d("CheckResponse", "Dailog Launcher " + "In Handler 4");

                        bingMapsView.getLayerManager().addLayer(routeLayer);
                        routeLine = null;

                        routeLayer.updateLayer();

                        if (route.BoundingBox != null) {
                            bingMapsView.setMapView(route.BoundingBox);
                            Log.d("CheckResponse", "Dailog Launcher " + "In Handler 5");
                        }

                        route = null;
                        msg.obj = null;
                    }

                    Message v = new Message();
                    v.arg1 = 0;
                    Log.d("CheckResponse", "Dailog Launcher " + "In Handler 6");
                    loadingScreenHandler.sendMessage(v);
                }
            };

            Log.d("CheckResponse", "Dailog Launcher " + "Route Request rr executed");
            RouteRequest rr = new RouteRequest();
            rr.addWaypoint(fromText);
            rr.addWaypoint(toText);
            rr.setRoutePathOutput(RoutePathOutput.Points);

            Log.d("CheckResponse", "Dailog Launcher " + "Route Request rr end & bmsservice.RouteAsync executed");

            bmService.RouteAsync(rr);
        } catch (Exception e) {
            Message v = new Message();
            v.arg1 = 0;
            loadingScreenHandler.sendMessage(v);
            Log.d("CheckResponse", "Dailog Launcher " + "exception Occured");
        }
    }
}

}

任何人都可以看看吗?

0 个答案:

没有答案