gpt4 book ai didi

android - 折线未完成到谷歌地图上的目的地

转载 作者:行者123 更新时间:2023-11-30 00:31:14 25 4
gpt4 key购买 nike

我正在尝试让应用程序输入纬度和经度并计算到位置之间的距离并在到标记位置之间绘制多段线但多段线未完成绘制到其他目的地。这是我的代码`

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private TextView source;
private TextView destination;
private EditText sLatitude1;
private EditText sLongtiude1;
private EditText dLatitude2;
private EditText dLongtiude2;
private Button button;
private GoogleMap mMap;
boolean mapReady = false;
MarkerOptions place1;
MarkerOptions place2;
Location location;
double lati1;
double longi1;
double lati2;
double longi2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// find text that display distance
textView = (TextView) findViewById(R.id.distance);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// find edit text and text view
source = (TextView) findViewById(R.id.sourc);
destination = (TextView) findViewById(R.id.destination);
sLatitude1 = (EditText) findViewById(R.id.lat1);
sLongtiude1 = (EditText) findViewById(R.id.long1);
dLatitude2 = (EditText) findViewById(R.id.lat2);
dLongtiude2 = (EditText) findViewById(R.id.long2);
// find button
button = (Button) findViewById(R.id.getDistance);

//在这里我将折线添加到 Onclick

  button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// find string from edittext
String lat1 = sLatitude1.getText().toString();
// parse string to double
lati1 = ParseDouble(lat1);
String lon1 = sLongtiude1.getText().toString();
longi1 = ParseDouble(lon1);
String lat2 = dLatitude2.getText().toString();
lati2 = ParseDouble(lat2);
String lon2 = dLongtiude2.getText().toString();
longi2 = ParseDouble(lon2);
Log.i("**lat", lat2);
CameraPosition place = CameraPosition.builder()
.target(new LatLng(lati1, longi1))
.zoom(3)
.bearing(6)
.tilt(45)
.build();
flyTo(place);

double xy1 = distanceBetween(new LatLng(lati1, longi1), new LatLng(lati2, longi2));
String distanceis = formatNumber(xy1);
textView.setText(distanceis);
place1= new MarkerOptions().position(new LatLng(lati1,longi1))
.title("i am here").icon(BitmapDescriptorFactory.fromResource(R.drawable.bubble_mask));
place2= new MarkerOptions().position(new LatLng(lati2,longi2))
.title("my destination").icon(BitmapDescriptorFactory.fromResource(R.drawable.bubble_mask));



mMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(lati1, longi1))
.add(new LatLng(lati2, lati2)));
// Add a listener for polyline clicks that changes the clicked polyline's color.
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
// Flip the values of the red, green and blue components of the polyline's color.
polyline.setColor(polyline.getColor() ^ 0x00ffffff);
}
});
mMap.addCircle(new CircleOptions()
.center(new LatLng(lati1, longi1))
.radius(5000)
.strokeColor(Color.GREEN)
.fillColor(Color.argb(54, 99, 255, 0)));
mMap.addCircle(new CircleOptions()
.center(new LatLng(lati2, longi2))
.radius(5000)
.strokeColor(Color.BLUE)
.fillColor(Color.argb(54, 99, 255, 0)));
mMap.addMarker(place1);
mMap.addMarker(place2);
}

});


}
// mehtod to parse double from string

double ParseDouble(String strNumber) {
if (strNumber != null && strNumber.length() > 0) {
try {
return Double.parseDouble(strNumber);
} catch (Exception e) {
return -1; // or some value to mark this field is wrong. or make a function validates field first ...
}
} else return 0;
}

// get distance
public static Double distanceBetween(LatLng point1, LatLng point2) {
if (point1 == null || point2 == null) {
return null;
}
double vw = SphericalUtil.computeDistanceBetween(point1, point2);
Log.i("distance isby utillib ", String.valueOf(vw));
return vw;
}

private String formatNumber(double distance) {
String unit = "m";
if (distance < 1) {
distance *= 1000;
unit = "mm";
} else if (distance > 1000) {
distance /= 1000;
unit = "km";
}

return String.format("%4.3f%s", distance, unit);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mapReady = true;
mMap = googleMap;}

//不知道为什么不完整

最佳答案

您以错误的方式添加了折线!

 mMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(lati1, longi1))
.add(new LatLng(lati2, lati2)));

在 lat2,lat2 中添加了一个点!

 mMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(lati1, longi1))
.add(new LatLng(lati2, longi2)));

关于android - 折线未完成到谷歌地图上的目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303894/

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