gpt4 book ai didi

android - 我可以在 Google Maps Android 中画一条弯曲的虚线吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:07 28 4
gpt4 key购买 nike

在浏览器的 Google map 中,弯曲的虚线如下所示: enter image description here

但是当我在自己的Android项目中实现Google Maps时,并没有显示这一行

enter image description here

如何画这条线?

最佳答案

您可以在两点之间实现曲线虚折线。为此,您可以使用 Google Maps Android API Utility Library具有 SphericalUtil 类并在您的代码中应用一些数学来创建折线。

你必须在你的 gradle 中包含实用程序库

编译 'com.google.maps.android:android-maps-utils:0.5'

请查看我的示例 Activity 和函数 showCurvedPolyline (LatLng p1, LatLng p2, double k),它在两点之间构造虚曲线折线。最后一个参数 k 定义折线的曲率,它可以是 >0 和 <=1。在我的示例中,我使用了 k=0.5

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private LatLng sydney1;
private LatLng sydney2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

mMap.getUiSettings().setZoomControlsEnabled(true);

// Add a marker in Sydney and move the camera
sydney1 = new LatLng(-33.904438,151.249852);
sydney2 = new LatLng(-33.905823,151.252422);

mMap.addMarker(new MarkerOptions().position(sydney1)
.draggable(false).visible(true).title("Marker in Sydney 1"));
mMap.addMarker(new MarkerOptions().position(sydney2)
.draggable(false).visible(true).title("Marker in Sydney 2"));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney1, 16F));

this.showCurvedPolyline(sydney1,sydney2, 0.5);
}

private void showCurvedPolyline (LatLng p1, LatLng p2, double k) {
//Calculate distance and heading between two points
double d = SphericalUtil.computeDistanceBetween(p1,p2);
double h = SphericalUtil.computeHeading(p1, p2);

//Midpoint position
LatLng p = SphericalUtil.computeOffset(p1, d*0.5, h);

//Apply some mathematics to calculate position of the circle center
double x = (1-k*k)*d*0.5/(2*k);
double r = (1+k*k)*d*0.5/(2*k);

LatLng c = SphericalUtil.computeOffset(p, x, h + 90.0);

//Polyline options
PolylineOptions options = new PolylineOptions();
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dash(30), new Gap(20));

//Calculate heading between circle center and two points
double h1 = SphericalUtil.computeHeading(c, p1);
double h2 = SphericalUtil.computeHeading(c, p2);

//Calculate positions of points on circle border and add them to polyline options
int numpoints = 100;
double step = (h2 -h1) / numpoints;

for (int i=0; i < numpoints; i++) {
LatLng pi = SphericalUtil.computeOffset(c, r, h1 + i * step);
options.add(pi);
}

//Draw polyline
mMap.addPolyline(options.width(10).color(Color.MAGENTA).geodesic(false).pattern(pattern));
}

}

您可以从 GitHub 下载包含完整代码的示例项目

https://github.com/xomena-so/so43305664

只需在 app/src/debug/res/values/google_maps_api.xml

中用您的 API key 替换我的 API key

enter image description here

关于android - 我可以在 Google Maps Android 中画一条弯曲的虚线吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305664/

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