我正在尝试在谷歌地图上绘制路线。我每2秒获得一次位置更新,但我无法在谷歌地图的实际道路上获得路线。我的路线在实际道路旁边,如何在实际道路上绘制线路???我正在使用服务类来获取位置更新并每2秒更新一次活动。但是路线没有进入谷歌地图的实际路径。谢谢提前。
我尝试了以下代码:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
public static final String MY_SERVICE = "names";
public static final String MY_SERVICE2 = "ids";
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
double speed,time;
public static Bundle names=new Bundle();
public static ArrayList<Object> loclist = new ArrayList<Object>();
final Handler handler = new Handler();
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES =10; // 10 meters
// The minimum time between updates in milliseconds
//private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * (2/60); // for
// 2
// secs
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5; // 5 minuite
//private static final long MIN_TIME_BW_UPDATES = 0;*/
// Declaring a Location Manager
protected LocationManager locationManager;
private int LOG_SIZE = 5;
private Intent intent;
private boolean isPassieveProvider;
public static int count = 0;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
showSettingsAlert(this);
} else {
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
String s=DateFormat.getTimeInstance().format(new Date());
Log.d("gps--->","time"+s+"\nlatitude:"+latitude+"longitude:"+longitude);
// mycodeforupdates(location);
}
}
}
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("newtwork--->","latitude:"+latitude+"longitude:"+longitude);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public boolean isUsable(Location location)
{
return location!=null && location.hasAccuracy() && location.getAccuracy() < 800 && location.getSpeed() > 0;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert(final Context mContext) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS/Network Enabling settings");
// Setting Dialog Message
alertDialog
.setMessage("GPS/Network is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
this.location = location;
getLatitude();
getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
final String tvTxt;
switch (status) {
case LocationProvider.AVAILABLE:
Log.d("status","status"+ "GPS available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.d("status","status" + "GPS out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.d("status","status" + "GPS temporarily unavailable\n");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
我的活动课程是:
:
switch (v.getId()) {
case R.id.play:
// creating the object of gps class
gps = new GPSTracker(getActivity());
// if gps can get location then start service otherwise showing an
// alert to enable gps.
if (gps.canGetLocation()) {
// gps enabled
getActivity().startService(startIntent);
if (playclickcount == 0) {
// if the user first time to click on play button clearing
// the previous values and google map
clearingArrays();
}
// calling the scheduler for every 2 seconds
callingScheduler();
} else {
// showing an alert to enable gps
gps.showSettingsAlert(getActivity());
}
及以下是我的计划代码,用于更新活动和绘制路线:
// calling scherduler for very 2 seconds
private void callingScheduler() {
// TODO Auto-generated method stub
// gps = new GPSTracker(getActivity());
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
gps = new GPSTracker(getActivity());
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(
getActivity(),
"Your Location is - \nLat: " + latitude
+ "\nLong: " + longitude,
Toast.LENGTH_SHORT).show();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
RouteManager.sourceMarker(getActivity(), latLng,
googleMap, markpos, 0);
markpos++;
points.add(latLng);// addding Latlng to points array
Log.d("pointssize", "pointskal--->" + points.size());
Double distance = 0.0;
//if more than one point we can find duplicate points getting or not,if getting duplicate we remove duplicate point
if (points.size() > 1) {
// 2.comparing prev and nextlatitudes and longitudes
boolean duplicate = routemanage.isDuplicate(points,
googleMap);
if (duplicate) {
points.remove(points.size() - 1);
Log.d("pointssize", "pointskalremnoveafter--->"
+ points.size());
duplicate = false;
} else {
int size = points.size();
//if not duplicate we can find out the distance
distance = routemanage.findDistanceOnRoute(
points.get(size - 2).latitude,
points.get(size - 2).longitude,
points.get(size - 1).latitude,
points.get(size - 1).longitude);
Log.d("sorrry", "distance initaial" + distance);
if (distance < 1) {
//if distance is less than 1 meter we simply remove the point
points.remove(points.size() - 1);
Log.d("pointssize",
"distance lessthan 1--->"
+ points.size());
} else {
// we have to check distance >1 m we can draw the route
sum = sum + distance;
RouteManager.drawRoute(getActivity(),
points, googleMap, sum);
Log.d("sorrry", "sorry dad sum" + sum);
settingmaprunDetails(points.get(size - 1),
sum);
}
duplicate = true;
}// else
}// if
//if points size is <=1 we can't draw the route
else if (points.size() == 1) {
settingmaprunDetails(points.get(0), 0.0);
}
}