我正在尝试使用nutiteq,因为我发现它是在3d地图上工作的更好的选择。我想学习它的每一点。所以我正在制作一个简单的项目,我可以从它的当前位置计算速度距离和时间同时跟踪它。 我申请了一些逻辑但失败了。你们任何人都可以帮助我。
public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
public int calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {
double latDistance = Math.toRadians(userLat - venueLat);
double lngDistance = Math.toRadians(userLng - venueLng);
double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
(Math.cos(Math.toRadians(userLat))) *
(Math.cos(Math.toRadians(venueLat))) *
(Math.sin(lngDistance / 2)) *
(Math.sin(lngDistance / 2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (int) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));
}
这是我到目前为止所使用的,并且正在调用此函数onLocationChange。
请帮忙。提前致谢
答案 0 :(得分:2)
这是我的代码,运作良好:
case R.id.btn_StartTrack:
location_manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = location_manager.getBestProvider(criteria, false);
//location.getAccuracy();
if (location_manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
if (!isclickedBtnStartTrackFirstTym)
{
for (int i = 0; i < geoLayerList.size(); i++)
{
mapView.getLayers().removeLayer(geoLayerList.get(i));
}
for (int i = 0; i < markerlist.size(); i++)
{
mapView.getLayers().removeLayer(markerlist.get(i));
}
for (int i = 0; i < markerlist1.size(); i++)
{
mapView.getLayers().removeLayer(markerlist1.get(i));
}
geoLayerList = new ArrayList<GeometryLayer>();
geoLayerList.clear();
markerlist = new ArrayList<MarkerLayer>();
markerlist.clear();
markerlist1 = new ArrayList<MarkerLayer>();
markerlist1.clear();
journeyDuration = 0;
totalSpeed = 0.0;
averageSpeed = 0.0;
distanceInMiles = 0.0;
distanceInMilesRound=0.0;
mtvCalcTime.setText("00:00");
mtvCalcDistance.setText("0:00");
mtvCalcSpeed.setText("0.0");
isclickedBtnStartTrackFirstTym = true;
btn_startTrack.setBackgroundResource(R.drawable.stop_tracking);
java.util.Date date = new java.util.Date();
journey_id = String.valueOf(date.getTime());
location_manager.requestLocationUpdates(provider, 1000, 1, activity);
startJourneyTimer();
}
else
{
location_manager.removeUpdates(activity);
if (!arr_lat_long.isEmpty())
{
last_pos = arr_lat_long.get(arr_lat_long.size() - 1);
Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.pointer);
MarkerStyle markerStyle = MarkerStyle.builder().setBitmap(pointMarker).setSize(0.5f).setAllowOverlap(false).setColor(Color.WHITE).build();
Label markerLabel = new DefaultLabel("");
markerLayer1 = new MarkerLayer(mapView.getComponents().layers.getBaseProjection());
Marker marker = new Marker(last_pos, markerLabel, markerStyle, null);
markerLayer1.add(marker);
markerlist.add(markerLayer1);
mapView.getLayers().addLayer(markerLayer1);
}
btn_startTrack.setBackgroundResource(R.drawable.start_tracking);
mLinearLayout.setVisibility(View.GONE);
mlinbtns.setVisibility(View.VISIBLE);
layoutVisibility = "2";
isclickedBtnStartTrackFirstTym = false;
if (timer != null)
{
timer.cancel();
}
}
}
else
{
Utilities.buildAlertMessageNoGps(getActivity());
}
break;
}
/**
*
* onLoctionChanged
*/
@Override
public void onLocationChanged(Location location)
{
double accuracy = location.getAccuracy();
if(accuracy<=30)
{
Tripname = "tripname";
Double newLat = location.getLatitude();
Double newLong = location.getLongitude();
lat_current = location.getLatitude();
lng_current = location.getLongitude();
if (endLatitude == -180)
{
endLatitude = newLat;
endLongitude = newLong;
}
else
{
startLatitude = endLatitude;
startLongitude = endLongitude;
endLatitude = newLat;
endLongitude = newLong;
}
double speed = (double) location.getSpeed();
speedInmph = JourneyUtils.getSpeedInMiles(speed); // method for speed t mph and kmph
totalSpeed = averageSpeed + speedInmph;
averageSpeed = totalSpeed / 2;
averageSpeed = Utilities.roundTwoDecimals(averageSpeed);
mtvCalcSpeed.setText(averageSpeed + "");
if (startLatitude != -180)
{
double distance = JourneyUtils.getDistanceInMiles(startLatitude, startLongitude, endLatitude, endLongitude);
distanceInMiles = distance + distanceInMiles;
distanceInMilesRound = Utilities.roundTwoDecimals(distanceInMiles);
mtvCalcDistance.setText(distanceInMilesRound + "");
}
long newTimeInMillis = location.getTime();
String utcDateTime = Utilities.getUTCDateTimeFromMillis(newTimeInMillis);
//mtvCalcTime.setText(utcDateTime);
db_journeySave.insertjourney(journey_id, utcDateTime, newLat + "", newLong + "", distanceInMiles + "", speedInmph + "", accuracy + "", Tripname, 0, 0, localTime1 + ":00");
mapView.getCameraPos();
MapPos lineLocation = mapView.getLayers().getBaseProjection().fromWgs84(endLongitude, endLatitude);
arr_lat_long.add(lineLocation);
ArrayList<MarkerLayer> arr_markerlist = new ArrayList<MarkerLayer>();
if (arr_lat_long.size() > 1)
{
MapPos start_lat = arr_lat_long.get(0);
Bitmap pointMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.green_pointer);
MarkerStyle markerStyle = MarkerStyle.builder().setBitmap(pointMarker).setSize(0.5f).setAllowOverlap(false).setColor(Color.WHITE).build();
Label markerLabel = new DefaultLabel("");
MarkerLayer markerLayer = new MarkerLayer(mapView.getComponents().layers.getBaseProjection());
Marker marker = new Marker(start_lat, markerLabel, markerStyle, null);
markerLayer.add(marker);
markerlist.add(markerLayer);
mapView.getLayers().addLayer(markerLayer);
MapPos last_pos1 = arr_lat_long.get(arr_lat_long.size() - 1);
Bitmap movingMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.blue);
MarkerStyle markerStyle1 = MarkerStyle.builder().setBitmap(movingMarker).setSize(0.5f).setAllowOverlap(false).setColor(Color.WHITE).build();
Label markerLabel1 = new DefaultLabel("");
MarkerLayer markerLayer1 = new MarkerLayer(mapView.getComponents().layers.getBaseProjection());
Marker marker1 = new Marker(last_pos1, markerLabel1, markerStyle1, null);
markerLayer1.add(marker1);
mapView.getLayers().addLayer(markerLayer1);
arr_markerlist.add(markerLayer1);
if(arr_markerlist.size()>0)
{
markerLayer1.remove(marker1);
mapView.getLayers().removeLayer(markerLayer1);
}
geoLayer = new GeometryLayer(mapView.getComponents().layers.getBaseProjection());
mapView.getLayers().addLayer(geoLayer);
geoLayerList.add(geoLayer);
StyleSet<LineStyle> lineStyleSet = new StyleSet<LineStyle>(LineStyle.builder().setWidth(0.15f).setColor(Color.CYAN).build());
ArrayList<MapPos> arr1_lat_long = new ArrayList<MapPos>();
arr1_lat_long.add(arr_lat_long.get(arr_lat_long.size() - 2));
arr1_lat_long.add(arr_lat_long.get(arr_lat_long.size() - 1));
line = new Line(arr1_lat_long, null, lineStyleSet, null);
geoLayer.add(line);
}
}
else
{
}
}