在我的应用程序中,我有包含 imagview 的 recyclerview,并且此 imageview 通过使用我存储在 sqlite 中的坐标包含静态 map 图像。当我单击该图像时,我将该坐标以字符串数组格式传递给其他 map Activity ,然后再次使用此字符串数组坐标绘制静态 map 保存到谷歌地图中的相同多边形。但我不明白该怎么做。
我尝试了以下代码但无法正常工作:
这是我的适配器类的代码,我在 ImageView 上显示静态 map ,然后使用 Intent 将坐标传递给 map Activity
String url ="https://maps.googleapis.com/maps/api/staticmap?";
url+="&zoom=18";
url+="&size=300x300";
url+="&maptype=satellite";
url+="&path=color:green|fillcolor:0xFFFF0033|"+ Coordinates;
url+="&key=" + "AIzaSyCj80x6E****Lx_KFsHKlogV0";
Picasso.get().load(url).into(holder.poly_image);
holder.poly_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mCtx, EditMapsActivity.class);
i.putExtra("img", Poly_Coords);
mCtx.startActivity(i);
}
});
这是我想使用坐标绘制多边形的 map Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_maps);
// Obtain the SupportMapFragment and get notified when the map is ready
to be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.mapView);
mapFragment.getMapAsync(EditMapsActivity.this);
Intent intent = getIntent();
String staticPolyCoords = intent.getStringExtra("img");
Log.d("Log", "Polygon Coords" + staticPolyCoords);
String answer = staticPolyCoords;
ArrayList<Location> arrayListLatLng = new ArrayList<>();
answer = answer.replace("lat/lng: (" , "");
answer = answer.replace(")" , "");
answer = answer.replace("]","");
answer = answer.replace("[","");
String[] arrayLatLng = answer.split(",");
Log.d("LOG_TAG", "Polygon Coordinates" + arrayLatLng);
for(int i = 0 ; i < arrayLatLng.length ; i++ ){
LatLng Cooordinate_Point = new
LatLng((Double.parseDouble(arrayLatLng[i])),
Double.parseDouble(arrayLatLng[i+1]));
Log.d("LOG_TAG", "Polygon Coordinates" + Cooordinate_Point);
latLngList.add(Cooordinate_Point);
}
然后在 map 准备方法()
@Override
public void onMapReady(GoogleMap googleMap) {
myMap = googleMap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
DrawPolygon(latLngList);
}
private void DrawPolygon(List<LatLng> latLngList) {
/* myMap.addPolygon(new PolygonOptions().strokeColor(Color.GREEN).fillColor(0x7F228B22).add(latLngList));*/
Polygon polygon = myMap.addPolygon(new PolygonOptions()
.clickable(true)
.add(latLngList));
// Store a data object with the polygon, used here to indicate an arbitrary type.
polygon.setTag("alpha");
polygon.setStrokeWidth(3);
polygon.setStrokeColor(Color.GREEN);
polygon.setFillColor(0x7F228B22);
}
最佳答案
如果您想在谷歌地图的应用程序中加载您的 map ,那么您可以使用以下方法:
String geoUri = "http://maps.google.com/maps?q=loc:" + latitude + "," + longitude + " (" + mTitle + ")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
context.startActivity(intent);
或者,如果您想在您的应用程序中加载它,请尝试使用 MapActivity,您需要先启用谷歌地图 API,然后您应用的坐标将显示在 map 上
请检查下面的链接,这将有助于集成
https://developers.google.com/maps/documentation/android-sdk/start
关于java - 如何在谷歌地图中使用 latlng 字符串数组绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61206574/