在 Android 应用程序中将 geojson 文件添加到我的地图
Posted
技术标签:
【中文标题】在 Android 应用程序中将 geojson 文件添加到我的地图【英文标题】:Add a geojson file to my Map in Android App 【发布时间】:2019-06-04 01:55:51 【问题描述】:我对 android Studio 和 Java 非常陌生。我尝试制作一个带有一些兴趣点的地图应用程序。这些点是一个geojson文件。我阅读了 google 的示例及其网站 (Google Maps Android GeoJSON Utility) 中的说明,但我的代码中有一些错误。第一个错误与 getmMap 方法有关,(错误:找不到符号方法 getnMap())。我找到解决方案的第二个是关于文件它不需要扩展名(geojson)。我在 rs 中创建了一个名为 raw 的文件夹,并在其中添加了 geojson 文件。
MapsActivity.java的完整代码
package com.example.vassilis.goldman_find_atm;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.data.geojson.GeoJsonLayer;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
private static final int MY_REQUEST_INT = 177;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
//Enable Current Location:
//Here We want to check the permission of Location - GPS
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(new String[]Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,MY_REQUEST_INT);
return;
else
//Here the code of Grand Permission
mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
/*LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
GeoJsonLayer layer = new GeoJsonLayer(getmMap(), R.raw.nbg_bank.geojson ,
getApplicationContext());
layer.addLayerToMap();
错误:cnnot find symbol method getmMap()
我将 getmMap() 更改为 mMap 和系统 :Surround with try/catch。我更改它并将代码更改为。代码的构建完成没有问题,但在地图上我没有看到点。
来自
GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.nbg_bank,
getApplicationContext());
layer.addLayerToMap();
收件人
GeoJsonLayer layer = null;
try
layer = new GeoJsonLayer(mMap, R.raw.nbg_bank,
getApplicationContext());
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
layer.addLayerToMap();
【问题讨论】:
只需使用mMap
而不是getmMap()
我改变了,但我必须从 GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.nbg_bank, getApplicationContext()); layer.addLayerToMap();到 GeoJsonLayer 层 = null;尝试 layer = new GeoJsonLayer(mMap, R.raw.nbg_bank, getApplicationContext()); catch (IOException e) e.printStackTrace(); catch (JSONException e) e.printStackTrace(); layer.addLayerToMap();然后鳕鱼建立了,但地图中没有点
【参考方案1】:
确保您的 json 文件格式正确。
检查此geojson
文件代码:
"type": "FeatureCollection",
"features": [
"type": "Feature",
"geometry":
"type": "Point",
"coordinates":
[102.0, 0.5]
,
"properties":
"prop0": "value0"
,
"type": "Feature",
"geometry":
"type": "LineString",
"coordinates": [
[102.0, 0.0],
[103.0, 1.0],
[104.0, 0.0],
[102.0, 0.0]
]
,
"properties":
"prop0": "value0",
"prop1": 0.0
]
更多信息请参考:https://cran.r-project.org/web/packages/geojsonR/vignettes/the_geojsonR_package.html
要检查您的 GeoJson 文件是否正确,请访问:http://geojsonlint.com/
【讨论】:
以上是关于在 Android 应用程序中将 geojson 文件添加到我的地图的主要内容,如果未能解决你的问题,请参考以下文章
python 在Python中将GeoJSON转换为WKT或从WKT转换。 #python #geojson #geometry
如何使用 Hibernate 在 PostGIS 中将 geojson(在 javascript 中)保存到几何(MultiPolygon,4326)