好的,所以我对MapBox还是很陌生,在此之前我曾经使用过GMaps,但是我发现MapBox能够满足我的需要,但问题是我碰壁了.
我已经在其网站上使用了示例的组合,例如
https://www.mapbox.com/android-sdk/examples/geocoding和
https://www.mapbox.com/android-sdk/examples/directions
我试图让用户搜索目的地,然后将其转换为坐标,以用于在地图上进行绘制.然后,我想画一条从他们当前位置到目的地的路线,这就是我的问题所在.
mapVIEw.getMapAsync(new OnMapReadyCallback() { @OverrIDe public voID onMapReady(MapBoxMap mapBoxMap) { map = mapBoxMap; // Set the origin waypoint to the devices location position origin = position.fromCoordinates(mapBoxMap.getMyLocation().getLongitude(), mapBoxMap.getMyLocation().getLatitude()); // Set the destination waypoint to the location point long clicked by the user final position destination = updateMap(feature.getLongitude(), feature.getLatitude()); mapBoxMap.addMarker(new MarkerOptions() .position(new LatLng(origin.getLatitude(), origin.getLongitude())) .Title("Origin") .snippet("Alhambra")); mapBoxMap.addMarker(new MarkerOptions() .position(new LatLng(latitude, destination.getLongitude())) .Title("Destination") .snippet("Plaza del Triunfo")); // Get route from API try { getRoute(origin, destination); } catch (ServicesException e) { e.printstacktrace(); } } }); AndroIDGeocoder geocoder = new AndroIDGeocoder(context, Locale.getDefault()); geocoder.setAccesstoken(MAPBox_ACCESS_TOKEN); addresses = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(), 1); // Set up autocomplete Widget GeocoderautoCompleteVIEw autocomplete = (GeocoderautoCompleteVIEw) findVIEwByID(R.ID.query); autocomplete.setAccesstoken("pk.eyJ1IjoiYmV1dHJveCIsImEiOiJjaW5ybzlwYnQwMGlqdnhtmno3cmtwNTlqIn0.y16mZzmertL4-eEfQNGeqQ"); autocomplete.setType(GeoCodingCriteria.TYPE_POI); autocomplete.setonFeatureListener(new GeocoderautoCompleteVIEw.OnFeatureListener() { @OverrIDe public voID OnFeatureClick(GeoCodingFeature feature) { position position = feature.asposition(); updateMap(position.getLatitude(), position.getLongitude()); } });}private voID getRoute(position origin, position destination) throws ServicesException { MapBoxDirections clIEnt = new MapBoxDirections.Builder() .setorigin(origin) .setDestination(destination) .setProfile(DirectionsCriteria.PROfile_CYCliNG) .setAccesstoken("pk.eyJ1IjoiYmV1dHJveCIsImEiOiJjaW5ybzlwYnQwMGlqdnhtmno3cmtwNTlqIn0.y16mZzmertL4-eEfQNGeqQ") .build(); clIEnt.enqueueCall(new Callback<DirectionsResponse>() { @OverrIDe public voID onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { // You can get the generic http info about the response Log.d(TAG, "Response code: " + response.code()); if (response.body() == null) { Log.e(TAG, "No routes found, make sure you set the right user and access token."); return; } // display some info about the route currentRoute = response.body().getRoutes().get(0); Log.d(TAG, "distance: " + currentRoute.getdistance()); Toast.makeText(MainActivity.this, "Route is " + currentRoute.getdistance() + " meters long.", Toast.LENGTH_SHORT).show(); // Draw the route on the map drawRoute(currentRoute); } @OverrIDe public voID onFailure(Call<DirectionsResponse> call, Throwable t) { Log.e(TAG, "Error: " + t.getMessage()); Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show(); } });}private voID drawRoute(DirectionsRoute route) { // Convert linestring coordinates into LatLng[] linestring linestring = linestring.frompolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5); List<position> coordinates = linestring.getCoordinates(); LatLng[] points = new LatLng[coordinates.size()]; for (int i = 0; i < coordinates.size(); i++) { points[i] = new LatLng( coordinates.get(i).getLatitude(), coordinates.get(i).getLongitude()); } // Draw Points on MapVIEw map.addpolyline(new polylineoptions() .add(points) .color(color.parsecolor("#009688")) .wIDth(5));}private voID myLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Todo: ConsIDer calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overrIDing // public voID onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } mapVIEw.setMyLocationEnabled(true); mapVIEw.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FolLOW); mapVIEw.getMyLocation();}
因此,这是我的问题,我无法在网上找到太多有关MapBox的信息,因为它们的新SDK与以前的版本有很大不同,主要是因为MapVIEw和MapBoxMap现在已分开(这仍然让我感到困惑).
任何帮助将不胜感激:D
:Edit:我主要是在设置目的地方面苦苦挣扎,因此,我当前的很多变量都混在一起了.如果您需要解释,我会很高兴.
解决方法:
您的代码中存在一些错误,最大的错误是,当您可以改用MapBoxMap时,仍在使用MapVIEw获取位置信息.这里有一些粗糙的代码可以满足您的要求.它不会像您应有的那样检查用户权限,但会向您显示如何执行MapBox的 *** 作.
import androID.app.Activity;import androID.graphics.color;import androID.os.Bundle;import androID.util.Log;import androID.Widget.Toast;import com.mapBox.mapboxsdk.annotations.MarkerOptions;import com.mapBox.mapboxsdk.annotations.polylineoptions;import com.mapBox.mapboxsdk.geometry.LatLng;import com.mapBox.mapboxsdk.maps.MapVIEw;import com.mapBox.mapboxsdk.maps.MapBoxMap;import com.mapBox.mapboxsdk.maps.OnMapReadyCallback;import com.mapBox.services.Constants;import com.mapBox.services.androID.geocoder.ui.GeocoderautoCompleteVIEw;import com.mapBox.services.commons.ServicesException;import com.mapBox.services.commons.geoJson.linestring;import com.mapBox.services.commons.models.position;import com.mapBox.services.directions.v5.DirectionsCriteria;import com.mapBox.services.directions.v5.MapBoxDirections;import com.mapBox.services.directions.v5.models.DirectionsResponse;import com.mapBox.services.directions.v5.models.DirectionsRoute;import com.mapBox.services.geoCoding.v5.GeoCodingCriteria;import com.mapBox.services.geoCoding.v5.models.GeoCodingFeature;import java.util.List;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;public class MainActivity extends Activity { private final static String TAG = "MainActivity"; private MapVIEw mapVIEw; private MapBoxMap map; private DirectionsRoute currentRoute; private position origin; private position destination; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Setup the MapVIEw mapVIEw = (MapVIEw) findVIEwByID(R.ID.mapVIEw); mapVIEw.onCreate(savedInstanceState); mapVIEw.getMapAsync(new OnMapReadyCallback() { @OverrIDe public voID onMapReady(MapBoxMap mapBoxMap) { map = mapBoxMap; mapBoxMap.setMyLocationEnabled(true); // Set up autocomplete Widget GeocoderautoCompleteVIEw autocomplete = (GeocoderautoCompleteVIEw) findVIEwByID(R.ID.query); autocomplete.setAccesstoken("<your access token>"); autocomplete.setType(GeoCodingCriteria.TYPE_POI); autocomplete.setonFeatureListener(new GeocoderautoCompleteVIEw.OnFeatureListener() { @OverrIDe public voID OnFeatureClick(GeoCodingFeature feature) { if(map.getMyLocation() != null) { // Set the origin as user location only if we can get their location origin = position.fromCoordinates(map.getMyLocation().getLongitude(), map.getMyLocation().getLatitude()); }else{ return; } destination = feature.asposition(); // Add origin and destination to the map map.addMarker(new MarkerOptions() .position(new LatLng(origin.getLatitude(), origin.getLongitude()))); map.addMarker(new MarkerOptions() .position(new LatLng(destination.getLatitude(), destination.getLongitude()))); // Get route from API try { getRoute(origin, destination); } catch (ServicesException e) { e.printstacktrace(); } } }); } });}private voID getRoute(position origin, position destination) throws ServicesException { MapBoxDirections clIEnt = new MapBoxDirections.Builder() .setorigin(origin) .setDestination(destination) .setProfile(DirectionsCriteria.PROfile_CYCliNG) .setAccesstoken("<your access token>") .build(); clIEnt.enqueueCall(new Callback<DirectionsResponse>() { @OverrIDe public voID onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { // You can get the generic http info about the response Log.d(TAG, "Response code: " + response.code()); if (response.body() == null) { Log.e(TAG, "No routes found, make sure you set the right user and access token."); return; } // Print some info about the route currentRoute = response.body().getRoutes().get(0); Log.d(TAG, "distance: " + currentRoute.getdistance()); Toast.makeText(MainActivity.this, "Route is " + currentRoute.getdistance() + " meters long.", Toast.LENGTH_SHORT).show(); // Draw the route on the map drawRoute(currentRoute); } @OverrIDe public voID onFailure(Call<DirectionsResponse> call, Throwable t) { Log.e(TAG, "Error: " + t.getMessage()); Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show(); } });}private voID drawRoute(DirectionsRoute route) { // Convert linestring coordinates into LatLng[] linestring linestring = linestring.frompolyline(route.getGeometry(), Constants.OSRM_PRECISION_V5); List<position> coordinates = linestring.getCoordinates(); LatLng[] points = new LatLng[coordinates.size()]; for (int i = 0; i < coordinates.size(); i++) { points[i] = new LatLng( coordinates.get(i).getLatitude(), coordinates.get(i).getLongitude()); } // Draw Points on MapVIEw map.addpolyline(new polylineoptions() .add(points) .color(color.parsecolor("#009688")) .wIDth(5));}@OverrIDepublic voID onResume() { super.onResume(); mapVIEw.onResume();}@OverrIDepublic voID onPause() { super.onPause(); mapVIEw.onPause();}@OverrIDeprotected voID onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapVIEw.onSaveInstanceState(outState);}@OverrIDeprotected voID onDestroy() { super.onDestroy(); mapVIEw.onDestroy();}@OverrIDepublic voID onLowMemory() { super.onLowMemory(); mapVIEw.onLowMemory();}
希望这可以帮助!
总结以上是内存溢出为你收集整理的Java-Mapbox Android:如何获取从当前位置到您选择的目的地的路线?全部内容,希望文章能够帮你解决Java-Mapbox Android:如何获取从当前位置到您选择的目的地的路线?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)