gpt4 book ai didi

android - 如何在 Android 的谷歌地图上自由绘制手形

转载 作者:可可西里 更新时间:2023-11-01 11:43:02 26 4
gpt4 key购买 nike

我需要在 Google map 上绘制一个自由手形。

我试过了 - LINK但没有找到我要求的解决方案。

我需要实现这个 - enter image description here enter image description here

最佳答案

这是 XML 代码 -

   <fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<FrameLayout
android:id="@+id/fram_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</FrameLayout>

Java类代码-

 FrameLayout fram_map;
Boolean Is_MAP_Moveable = false; // to detect map is movable
private GoogleMap googleMap;
private boolean screenLeave = false;
int source = 0;
int destination = 1;
private ArrayList<LatLng> val = new ArrayList<LatLng>();

@Nullable
@Override
public View onCreateView(){
fram_map = (FrameLayout) view.findViewById(R.id.fram_map);


fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override

public boolean onTouch(View v, MotionEvent event) {

if (Is_MAP_Moveable) {
float x = event.getX();
float y = event.getY();

int x_co = Math.round(x);
int y_co = Math.round(y);

// Projection projection = googleMap.getProjection();
Point x_y_points = new Point(x_co, y_co);

LatLng latLng = googleMap.getProjection().fromScreenLocation(x_y_points);
latitude = latLng.latitude;

longitude = latLng.longitude;
HomeBean bean = new HomeBean();
bean.setPost_lat(String.valueOf(latitude));
bean.setPost_long(String.valueOf(longitude));
bean.setSource_id("3");

mLatLongList.add(bean);


System.out.println("LatLng : " + latitude + " : " + longitude);

LatLng point = new LatLng(latitude, longitude);

int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
// finger touches the screen
screenLeave = false;
// System.out.println("ACTION_DOWN");

// val.add(new LatLng(latitude, longitude));

case MotionEvent.ACTION_MOVE:
// finger moves on the screen
// System.out.println("ACTION_MOVE");
/* if (val.size()==3){
val.remove(1);
}*/

val.add(new LatLng(latitude, longitude));
screenLeave = false;
Draw_Map();

case MotionEvent.ACTION_UP:

// System.out.println("ACTION_UP");
if (!screenLeave) {
screenLeave = true;
} else {
System.out.println("ACTION_UP ELSE CAse");
Is_MAP_Moveable = false; // to detect map is movable
btn_draw_State.setImageResource(R.mipmap.erase_icon);
source = 0;
destination = 1;
draw_final_polygon();

}

// finger leaves the screen
// Is_MAP_Moveable = false; // to detect map is movable
// Draw_Map();
break;
default:
break;
}

if (Is_MAP_Moveable) {
Log.e("DRAW on MAP : ", "LatLng ArrayList Size : " + mLatLongList.size());
return true;

} else {
return false;
}


} else {
return false;
}


}
});
......}

public void Draw_Map() {


// specify latitude and longitude of both source and destination Polyline

if (val.size() > 1) {
googleMap.addPolyline(new PolylineOptions().add(val.get(source), val.get(destination)).width(8).color(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea)));
source++;
destination++;
}


}


private void draw_final_polygon() {

val.add(val.get(0));

PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(val);
polygonOptions.strokeColor(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea));
polygonOptions.strokeWidth(8);
polygonOptions.fillColor(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea));
Polygon polygon = googleMap.addPolygon(polygonOptions);
}

关于android - 如何在 Android 的谷歌地图上自由绘制手形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35425896/

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