【问题标题】:How can I show the GeoJSON Polygon in Android Studio mapView?如何在 Android Studio mapView 中显示 GeoJSON 多边形?
【发布时间】:2019-12-31 07:15:27
【问题描述】:

现在,我有一个 GeoJSON 文件和 Android mapView。我想要做的是在 mapView 上显示所有 Polygon 和 MultiPolygon 以及我在上一步中添加的标记。 (检查多边形区域的标记与否)

有 GeoJSON

{
"type": "FeatureCollection",
"name": "4_councils",
"crs": { "type": "name", "properties": { "name": 
"urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID": "P1", "Council": "Port Philip" 
}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 
144.983320716990988, -37.855649951415302 ], [ 144.989442599617007, 
-37.856463488857401 ], [ 144.989683215443989, -37.856496880476598 ], [ 
144.989802299748987, -37.856518915838002 ], [ 144.989920167567988, 
-37.856544354765902 ], [ 144.99003662904201, -37.856573283238298 ], [ 
144.990288366451011, -37.856645787042801 ], [ 144.990341200622993, 
-37.856667411620997 ], [ 144.99056726731601, -37.8567568655901 ], [ 
144.990677076416006, -37.856806402360398 ], [ 144.99081204674701, 
-37.856875109339597 ], [ 144.991071531670997, -37.857024777883801 ], [ 
144.991595871839991, -37.857333575320901 ], [ 144.991835803804008, 
-37.857464799476503 ], [ 144.992082337905003, -37.857573790264702 ], [ 
144.992269516768005, -37.857649333054603 ], [ 144.992599822361001, 
-37.857759030725902 ], [ 144.992861025173994, -37.857828356167303 ], [ 
144.992993687883995, -37.857857828362 ], [ 144.993127475098987, 
-37.857883896020603 ], [ 144.993449684696998, -37.857932816884201 ], [ 
144.993746511793006, -37.857974186756003 ], [ 144.995321462850001, 
-37.858184912941098 ], [ 144.997383419530991, -37.858464812753802 ], [ 
144.999665383186993, -37.858775265328298 ], [ 145.001366209585001, 
-37.859004537944799 ], [ 145.004151931436013, -37.859376074514003 ], [ 
145.010521319351, -37.860225332883999 ], [ 145.009878228194992, 
-37.863745450239001 ], [ 145.009164298976003, -37.867596379628203 ], [ 
145.000035673882991, -37.866455477900502 ], [ 144.999446421478012, 
-37.869516849689099 ], [ 144.991503802349001, -37.868513288453599 ], [ 
144.987026831858998, -37.867940446644702 ], [ 144.98122634874801, 
-37.867169588976601 ], [ 144.981778633548998, -37.864098581783203 ], [ 
144.982492348128005, -37.860199626728203 ], [ 144.982832416880996, 
-37.858373203992201 ], [ 144.98285541551499, -37.858172496951497 ], [ 
144.982853341010014, -37.858011364166998 ], [ 144.982808576574996, 
-37.857614702823803 ], [ 144.983013618168002, -37.856742364949 ], [ 
144.983206241613004, -37.855634294902998 ], [ 144.983320716990988, 
-37.855649951415302 ] ] ] } },
{ "type": "Feature", "properties": { "ID": "M1", "Council": "Melbourne" }, 
"geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 
144.934883624500003, -37.794983255851001 ], [ 144.930911282918998, 
-37.794530670070003 ], [ 144.929578050644011, -37.788603834271001 ], [ 
144.930964167907007, -37.788762099296001 ], [ 144.931198145182009, 
-37.788760005896002 ], [ 144.931994165453006, -37.788850451712001 ], [ 
144.932088361960012, -37.788346519561003 ], [ 144.932170970375012, 
-37.788335039045997 ], [ 144.93604338541499, -37.788749332843999 ], [ 
144.936043561756009, -37.788822899784002 ], [ 144.934883624500003, 
-37.794983255851001 ] ] ] ] } },
]
}

这是我的 Android JAVA 课程,其中包含从之前的活动中获得的经纬度。 mapView 在此页面上。 GeoJSON 文件现在存储在 assets 文件夹中。

public class timetable extends AppCompatActivity implements OnMapReadyCallback {
String address;
Double Lat;
Double Long;
private MapView mMapView;
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timetable);

    Bundle mapViewBundle = null;
    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
    }
    mMapView = (MapView) findViewById(R.id.mapView);
    mMapView.onCreate(mapViewBundle);

    mMapView.getMapAsync(this);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    address = bundle.getString("address");
    Lat = bundle.getDouble("Lat");
    Long = bundle.getDouble("Long");
    final TextView tvMyLocation = (TextView) findViewById(R.id.tvMyLocation);
    final TextView LatLong = (TextView) findViewById(R.id.LatLong);
    tvMyLocation.setText("Yuor current adress is " + address);
    LatLong.setText("Yuor current Latitude and Longitude is " + Lat + "  " + Long);


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
    if (mapViewBundle == null) {
        mapViewBundle = new Bundle();
        outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
    }

    mMapView.onSaveInstanceState(mapViewBundle);
}

@Override
protected void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
protected void onStart() {
    super.onStart();
    mMapView.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    mMapView.onStop();
}

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions().position(new LatLng(Lat, Long)).title("Your set address"));
    LatLng latLng = new LatLng(Lat,Long);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

}

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

}

【问题讨论】:

    标签: android geojson android-mapview


    【解决方案1】:

    终于解决了

    将 onMapReady 方法改为

    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions().position(new LatLng(Lat, Long)).title("Your set address is: " + address));
        LatLng latLng = new LatLng(Lat,Long);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
        GeoJsonLayer layer = null;
        try {
            layer = new GeoJsonLayer(map, R.raw.GeoJSONfileName, getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        layer.addLayerToMap();
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多