gpt4 book ai didi

android - 如何在静态 View 的中心点与谷歌地图标记之间画一条线?

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

我的 xml 文件中有一个静态线性布局和一个谷歌地图。我想在线性布局的中心底部与标记位置之间绘制一条直线。我尝试在 map 中使用多段线,但它也在 map 中的标记之间绘制。下面是我的 xml 文件 -

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rel_m1"
android:layout_above="@+id/bottom_ll"
android:id="@+id/frameLayout">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.business.introslider.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:id="@+id/pickup_ll"
android:onClick="OpenPickup"
android:elevation="30dp"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pickup from"
android:layout_marginTop="5dp"
android:textSize="14sp"
android:textColor="#555555"
android:padding="5dp"
android:drawableLeft="@drawable/s_green"
android:drawablePadding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pin location"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:textColor="#000000"
android:padding="5dp"
android:id="@+id/text_pickup_location"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</FrameLayout>

还有我的 java 代码如下 -

public void setCurrentLocation(){
try {
//to show my current location in the map

mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
}
});
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//to request location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
ViewTreeObserver vto = pickup_ll.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
pickup_ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
pickup_ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
float x = pickup_ll.getX() + pickup_ll.getMeasuredWidth() / 2;
float y = pickup_ll.getY() + pickup_ll.getMeasuredHeight() / 2;

// float x= getWindowManager().getDefaultDisplay().getWidth()/2;
point1 = new Point(Math.round(x),Math.round(y));
Log.e(TAG, "point1===" + point1);
LatLng latLng1 = mMap.getProjection().fromScreenLocation(point1);
Log.e(TAG,"latLng1==="+latLng1);
points.add(latLng1);
}

});

@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onlocation changed");
//To clear map data
mMap.clear();
//To hold location
if(latLng !=null) {
Log.e(TAG, "latLng onlocation changed==" + latLng);
}
else {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
//to create a marker in the app
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Location");
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.BLACK);
polylineOptions.width(2);
points.add(latLng);
polylineOptions.addAll(points);
mMap.addPolyline(polylineOptions);
mMap.addMarker(markerOptions);

//opening position with some zoom level in the app
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
}

我在 googleApiClient 的 onMapReady 方法中调用这个 setCurrentLocation()。任何帮助将不胜感激。

最佳答案

你试过这样画路径吗? https://github.com/hiepxuan2008/GoogleMapDirectionSimple/这会让你在两个地方之间画一条路径。确保更改 API key 。

关于android - 如何在静态 View 的中心点与谷歌地图标记之间画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42410176/

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