gpt4 book ai didi

android - 如何在 map 上显示路线

转载 作者:行者123 更新时间:2023-11-30 02:37:18 24 4
gpt4 key购买 nike

我想创建一个显示 map 的 Activity 。当我点击一个按钮时,我将一个 LatLng 点保存到列表中。所以我走在街上,当我走路时,我点击按钮,我想在 map 上显示我的路线。所以我尝试编写这段代码,但没有找到:

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);
}
}

我们能帮我吗?

最好的问候

最佳答案

我已经解决了我的问题,代码是这样的:

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.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_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 - 如何在 map 上显示路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com