如何在地图上显示路线
Posted
技术标签:
【中文标题】如何在地图上显示路线【英文标题】:How to display a routes on map 【发布时间】:2014-10-10 09:41:21 【问题描述】:我想创建一个显示地图的活动。当我点击一个按钮时,我将一个 LatLng 点保存到列表中。所以我走在一条街上,当我走的时候,我点击按钮,我想在地图上显示我的路线。所以我尝试编写这段代码,但没有找到:
public void settaMappa(View view)
LocationResult locationResult = new LocationResult()
@Override
public void gotLocation(Location location)
//Got the location!
System.out.println("ho avuto un segnale gps valido ");
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
if(listaPuntiTerreno == null)
listaPuntiTerreno = new ArrayList<LatLng>();
if (location != null)
LatLng latLong = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
listaPuntiTerreno.add(latLong);
disegnaTracciato();
;
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
public void disegnaTracciato()
if(listaPuntiTerreno !=null &&
listaPuntiTerreno.size()>1)
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < listaPuntiTerreno.size(); z++)
LatLng point = listaPuntiTerreno.get(z);
options.add(point);
Polyline line = mMap.addPolyline(options);
我们能帮帮我吗?
最好的后卫
【问题讨论】:
你的问题到底是什么? 想用您的特定路线为您当前的位置设置动画吗? 问题是这样的:当我输入地图 PolylineOptions 但我在地图上看不到规则时。 【参考方案1】:我已经解决了我的问题,代码是这样的:
public MyLocation myLocation;
/**
* il metodo consente di registrare il punto
*/
LocationResult locationResult = new LocationResult()
@Override
public void gotLocation(Location location)
//Got the location!
System.out.println("ho avuto un segnale gps valido ");
if (location != null)
LatLng latLong = new LatLng(location.getMyLocation().getLatitude(),
location.getMyLocation().getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 25));
listaPuntiTerreno.add(latLong);
Toast toast = Toast.makeText(getApplicationContext(), "Punto registrato correttamente", Toast.LENGTH_SHORT);
toast.show();
disegnaTracciato();
;
当我点击按钮时,我调用了这个方法:
public void registraPunto(View view)
myLocation.getLocation(this, locationResult);
这是一个 MyLocation 类(我从另一个帖子中复制了这个类)
public class MyLocation
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
private ProgressDialog pDialog;
private final static int LOCALIZATION_DELAY = 10000;
public boolean getLocation(Context context, LocationResult result)
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
trygps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);catch(Exception ex)
trynetwork_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);catch(Exception ex)
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
pDialog = ProgressDialog.show(context, "Attendere ...", "Localizzazione in corso ...", true);
pDialog.setCancelable(false);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), LOCALIZATION_DELAY);
return true;
LocationListener locationListenerGps = new LocationListener()
public void onLocationChanged(Location location)
timer1.cancel();
if(pDialog.isShowing())
pDialog.dismiss();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
public void onProviderDisabled(String provider)
public void onProviderEnabled(String provider)
public void onStatusChanged(String provider, int status, Bundle extras)
;
LocationListener locationListenerNetwork = new LocationListener()
public void onLocationChanged(Location location)
timer1.cancel();
if(pDialog.isShowing())
pDialog.dismiss();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
public void onProviderDisabled(String provider)
public void onProviderEnabled(String provider)
public void onStatusChanged(String provider, int status, Bundle extras)
;
class GetLastLocation extends TimerTask
@Override
public void run()
if(pDialog.isShowing())
pDialog.dismiss();
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null)
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
if(gps_loc!=null)
locationResult.gotLocation(gps_loc);
return;
if(net_loc!=null)
locationResult.gotLocation(net_loc);
return;
locationResult.gotLocation(null);
public static abstract class LocationResult
public abstract void gotLocation(Location location);
现在,当我尝试单击按钮时,我从 MyLocation 类中获得了一个位置,但经度和纬度是错误的。如果我在 maps.google.it 上插入这个数字,我发现我的真实位置是错误的,如果我从 mMap 对象中获得经度和纬度,这些都是正确的。
我的错误在哪里? 我们可以帮帮我吗?
【讨论】:
以上是关于如何在地图上显示路线的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中的谷歌地图上绘制路线并计算多个标记之间的距离