如何在谷歌地图中使用latlng的字符串数组绘制多边形 [英] how to draw polygon using string array of latlng in google map

查看:198
本文介绍了如何在谷歌地图中使用latlng的字符串数组绘制多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有recyclerview,其中包含imagview,并且此imageview通过使用我存储在sqlite中的坐标包含静态地图图像.当我单击该图像时,我将该字符串数组格式的坐标传递给其他地图活动,然后再次使用此字符串数组坐标绘制将静态地图保存到Google地图中的同一多边形.但我不知道该怎么做.

In my app, i have recyclerview which contain imagview and this imageview contain static map image by using coordinates which i store in sqlite. and when i click on that image i am passing that coordinates in string array format to other map activity and then using this string array coordinates again draw the same polygon which static map hold into google map. but i don't understand how to do this.

我尝试了以下代码,但不起作用:

这是我的适配器类的代码,其中我在图像视图上显示静态地图,然后使用intent将坐标传递给地图活动

this is code of my adapter class where i display static map on image view and then using intent pass the coordinates to 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);
        }
    });

这是我的地图活动,我想使用坐标绘制多边形:

this is my map activity where i want to draw polygon using coordinates:

 @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);
    }

然后使用地图就绪方法()

then in map ready method()

 @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);

}

推荐答案

使用此代码进行转换:

    String answer = staticPolyCoords;
    answer = answer.replace("lat/lng:" , "");
    answer = answer.replace(")" , "]");
    answer = answer.replace("(" , "[");

    try {
        JSONArray jsonArray = new JSONArray(answer);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray latLong = jsonArray.getJSONArray(i);
            double lat = latLong.getDouble(0);
            double lon = latLong.getDouble(1);
            coordinates.add(new LatLng(lat, lon));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

然后在您的drawPolygon方法中为每个循环尝试此操作:

 PolygonOptions poly = new PolygonOptions();
    for (LatLng latLng : coordinates) {
        //use the coordinates.
        poly.addAll(Collections.singleton(latLng));
    }


    Polygon polygon = myMap.addPolygon(poly);
   //add here your set methods

    myMap.moveCamera(CameraUpdateFactory.newLatLng(coordinates.get(3)));
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
    myMap.animateCamera(zoom);
 }

这篇关于如何在谷歌地图中使用latlng的字符串数组绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆