java - 如何在谷歌地图中使用 latlng 字符串数组绘制多边形

标签 java android polygon google-static-maps

在我的应用程序中,我有包含 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/

相关文章:

java - 调用 Activity 的另一种方法。?

android - BLE 蓝牙低功耗设备中的身份验证问题

r - 识别位于纬度和经度坐标内的邮政编码

ruby-on-rails - MultiPolygon 列按迁移

android - Porter-Duff:不同形状的不同行为?

r - 如何使用 sf::st_centroid 计算多边形的质心?

java - Spring Data REST + JPA 从 OneToMany 集合中删除 [不是所有者端]

java - Graphql 联合在扩展关系上返回 null

java - JFR:读取 .jfr 文件时出现 OutOfMemoryError

java - 在 OpenGL ES 2.0 中绘制球体