2016-06-17 12 views
0

私はここに新しいですので、その質問が既に(normaly not)または初心者として質問されている場合sympaticしてください、多分私はそれを間違っている。Google MapsのGeoJson Center Android

だから問題があります。

私はGoogleマップアンドロイド、私のgeojsonを中心にしたいと思います。

私のgeoJsonがあります。

{ 
    "type": "Polygon", 
    "coordinates": [ 
     [ 
     [ 
      2.1096056699752808, 
      49.007069721997176 
     ], 
     [ 
      2.1101635694503784, 
      49.00640113120101 
     ], 
     [ 
      2.1103888750076294, 
      49.00579235387832 
     ], 
     [ 
      2.1109735965728755, 
      49.00497770668481 
     ], 
     [ 
      2.1093428134918213, 
      49.004180641564616 
     ], 
     [ 
      2.10762619972229, 
      49.003237519337205 
     ], 
     [ 
      2.1063923835754395, 
      49.00271668298914 
     ], 
     [ 
      2.104933261871338, 
      49.0023225329428 
     ], 
     [ 
      2.103109359741211, 
      49.00157645467159 
     ], 
     [ 
      2.1024978160858154, 
      49.00125268137821 
     ], 
     [ 
      2.102004289627075, 
      49.000830365223656 
     ], 
     [ 
      2.1010172367095947, 
      48.99976048160506 
     ], 
     [ 
      2.1006417274475098, 
      48.9995141235692 
     ], 
     [ 
      2.0986783504486084, 
      48.99873280859517 
     ], 
     [ 
      2.0958673954010005, 
      48.997838856609434 
     ], 
     [ 
      2.093796730041504, 
      48.99738131592137 
     ], 
     [ 
      2.0936572551727295, 
      48.99733204205815 
     ], 
     [ 
      2.09065318107605, 
      48.998359743969125 
     ], 
     [ 
      2.0894408226013184, 
      48.99889470369554 
     ], 
     [ 
      2.088373303413391, 
      48.99951060415987 
     ], 
     [ 
      2.0878690481185913, 
      49.00182632175247 
     ], 
     [ 
      2.0877134799957275, 
      49.002618145770015 
     ], 
     [ 
      2.087423801422119, 
      49.003913190807005 
     ], 
     [ 
      2.0922625064849854, 
      49.0047999956991 
     ], 
     [ 
      2.09663987159729, 
      49.00562344324437 
     ], 
     [ 
      2.101478576660156, 
      49.006489104187075 
     ], 
     [ 
      2.104707956314087, 
      49.007101391864836 
     ], 
     [ 
      2.1054375171661377, 
      49.0071295428414 
     ], 
     [ 
      2.1062850952148438, 
      49.00706620312175 
     ], 
     [ 
      2.108291387557983, 
      49.007059165370144 
     ], 
     [ 
      2.1096056699752808, 
      49.007069721997176 
     ] 
     ] 
    ], 
    "properties": { 
        "Territoire" : 2 
       } 
    } 

これを文字列にしてからJsonObjectにすることができます。このように:ここで

String json = loadJSONFromAsset(ville, terr); 



       try { 
        JSONObject obj = new JSONObject(json); 
        GeoJsonLayer layer = new GeoJsonLayer(mMap, obj); 
        layer.addLayerToMap(); 
        (......) 

      } catch (JSONException e) { 
        e.printStackTrace(); 


        } 

     public String loadJSONFromAsset(String ville, String terr) { 
       String json = null; 
       try { 
        InputStream is = getAssets().open(ville+"/"+terr+".geojson"); 
        int size = is.available(); 
        byte[] buffer = new byte[size]; 
        is.read(buffer); 
        is.close(); 
        json = new String(buffer, "UTF-8"); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
        return null; 
       } 
       return json; 
      } 

私は良いよよく私は.... :-D

と思うが、私は良いズームレベルで、私のGoogleマップで私の層を中心したいと思います知っています.. 。問題があります! 私はインターネットで時間を過ごしましたが、できません。私は何か試したが動作しません:

 JSONArray jsonArray = obj.getJSONArray("coordinates"); 
     ArrayList<LatLng> listdata = new ArrayList<LatLng>(); 

       if (jsonArray != null) { 
        for (int i=0;i<jsonArray.length();i++){ 
         String[] coord = jsonArray.toString().split(","); 
         double x = Double.parseDouble(coord[0]); 
         double y = Double.parseDouble(coord[1]); 
         listdata.add(new LatLng(x,y)); 
        } 
       } 

     LatLng center = getPolygonCenterPoint(listdata); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(center)); 


private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){ 
     LatLng centerLatLng = null; 
     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     for(int i = 0 ; i < polygonPointsList.size() ; i++) 
     { 
      builder.include(polygonPointsList.get(i)); 
     } 
     LatLngBounds bounds = builder.build(); 
     centerLatLng = bounds.getCenter(); 

     return centerLatLng; 
    } 

誰かが答えを知っていますか?

ありがとう!

ヨルダン

答えて

1

あなたが生成しているLatLngBounds boundsオブジェクト(documentation)の完全な拡張子にマップをセンタリングすることができます:あなたは(私のようにそれを設定しているパディングを設定することができます

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)); 

注意あなたのコードでは0)

private LatLngBounds getPolygonBounds(ArrayList<LatLng> polygonPointsList){ 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    for(int i = 0 ; i < polygonPointsList.size() ; i++) { 
     builder.include(polygonPointsList.get(i)); 
    } 
    return builder.build(); 
} 

センター:

LatLngBounds bounds = getPolygonBounds(listdata); 
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)); 
+0

ちょっと!あなたが助けてくれてありがとう!それはうまくいかないと思われますが、なぜか分からないのですが、コードでは私をソマリアの海に連れて行っています.... 多分間違った座標?? –

+0

気にしないでください!それは私だった、私は良い順序でそれを使用していない。 今はいいです、ありがとうございます! –

関連する問題