如何在Google地图中使用latlng的字符串数组绘制多边形

问题描述 投票:1回答:3

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

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

      holder.Category.setText(product.getFcategory());
    holder.Date.setText(product.getFdate());
    final String Poly_Coords = product.getfPolygon();

    String s = Poly_Coords.replaceAll("lat","");
    String s1 = s.replaceAll("lng","");
    String s2 = s1.replaceAll("/:","");
    String s3 = s2.replaceAll("\\[", "").replaceAll("\\]","");
    String s4 = s3.replaceAll("\\(", "").replaceAll("\\),","|");
    String s5 = s4.replaceAll(" ", "");
    final String Coordinates = s5.replaceAll("\\)", "");
    Log.d("LOG_TAG", "Polygon Coordinates" +  s1);


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

    final String finalUrl = url;
    holder.poly_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           /* Picasso.get().load(finalUrl).into(mapView);*/
            Intent i = new Intent(mCtx, EditMapsActivity.class);
            i.putExtra("img", Poly_Coords);
            mCtx.startActivity(i);
        }
    });

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

 @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(")" , "");
    String[] arrayLatLng = answer.split(",");
    Log.d("LOG_TAG", "Polygon Coordinates" +  arrayLatLng);

    for(int i = 0 ; i < arrayLatLng.length ; i++ ){

        Location j = new Location(poly_location);
        j.setLatitude(Double.parseDouble(arrayLatLng[i]));
        j.setLongitude(Double.parseDouble(arrayLatLng[i+1]));
        arrayListLatLng.add(new Location(j));
    }
    Location A  = arrayListLatLng.get(0);
    Log.d("LOG_TAG", "Polygon Coordinates" +  A);
java android polygon google-static-maps
3个回答
0
投票

从您的按钮单击侦听器调用此:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com/maps/@?api=1&map_action=map&center=" + Coordinates));
mCtx.startActivity(i);

来源:Maps URLs


0
投票

如果要在Google地图的应用程序中加载地图,则可以采用以下方法:

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,首先需要启用google map API,然后您应用的坐标将显示在地图上

请检查下面的链接,这将有助于整合此信息

https://developers.google.com/maps/documentation/android-sdk/start


0
投票

如果我正确理解了您的问题,则需要:首先显示静态地图,然后单击后显示相同的静态地图,但上面带有多个多边形。与测验的情况一样,当询问用户的第一个(问题)Activity正确的对象位置时,第二个(回答)Activity显示正确的对象位置。

为此,您需要:

1)将静态地图的图像绑定到大地坐标;

2)实现GoogleMap.addPolygon()方法在其上绘制多边形。

这两项任务通常都不是一件容易的事,所以最简单的方法是:

1]如果您想在第二个活动中显示快速正确的答案,请在您的第一个GoogleMap.addPolygon()中请求并从静态地图API中获取两张地图:第一张-仅使用地图图像,第二张-使用相同的地图,但已经使用了多边形。您在该活动中显示的第一张图像,到第二个Activity(单击后显示),则显示已经具有多边形的第二张图像。或者,如果只想显示单击的地图的答案,请在第二个Activity中请求第二个图像。在Activity中,您可以找到如何获取带有多边形的静态地图图像(请查看Official Documentation参数):

path

现在的地图,静态API URL为https://maps.googleapis.com/maps/api/staticmap ?size=400x400&center=40.653279,-73.959816&zoom=11 &path=fillcolor:0xAA000033%7Ccolor:0xFFFFFF00%7Cenc:}zswFtikbMjJzZ%7CRdPfZ}DxWvBjWpF~IvJnEvBrMvIvUpGtQpFhOQdKpz@bIx{A%7CPfYlvApz@bl@tcAdTpGpVwQtX}i@%7CGen@lCeAda@bjA%60q@v}@rfAbjA%7CEwBpbAd_@he@hDbu@uIzWcWtZoTdImTdIwu@tDaOXw_@fc@st@~VgQ%7C[uPzNtA%60LlEvHiYyLs^nPhCpG}SzCNwHpz@cEvXg@bWdG%60]lL~MdTmEnCwJ[iJhOae@nCm[%60Aq]qE_pAaNiyBuDurAuB}}Ay%60@%7CEKv_@?%7C[qGji@lAhYyH%60@Xiw@tBerAs@q]jHohAYkSmW?aNoaAbR}LnPqNtMtIbRyRuDef@eT_z@mW_Nm%7CB~j@zC~hAyUyJ_U{Z??cPvg@}s@sHsc@_z@cj@kp@YePoNyYyb@_iAyb@gBw^bOokArcA}GwJuzBre@i\tf@sZnd@oElb@hStW{]vv@??kz@~vAcj@zKa%60Atf@uQj_Aee@pU_UrcA &key=YOUR_API_KEY。因此,如果多边形有很多点,则应将restricted to 8192 characters in size用于多边形点。您可以使用encoded polyline formatPolyUtil.encode()方法对多边形进行编码。

2](IMHO更好),请使用Android版Maps SDK的PolyUtil.encode() Maps SDK for Android Utility Library,该服务可以提供地图的位图图像,从而为用户提供有限的交互性。此模式支持折线,您可以节省一些流量。在这种情况下,您应该在第一个活动中放置MapView/MapFragment,在第二个活动中放置具有相同设置(大小,相机位置等)的Lite Mode,并且当用户单击第一个活动的地图时,只需绘制第二个活动的地图的多边形并显示它。形状的精简模式功能与MapView' or 'MapFragment匹配:

MapView/MapFragment
© www.soinside.com 2019 - 2024. All rights reserved.