gpt4 book ai didi

android - 在 Android 应用程序中将 geojson 文件添加到我的 map

转载 作者:行者123 更新时间:2023-12-02 00:56:09 26 4
gpt4 key购买 nike

我对 Android Studio 和 Java 的任何事情都很陌生。我尝试制作一个带有一些兴趣点的 map 应用程序。这些点是一个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();
}
}

error: cnnot find symbol method getmMap()



我将 getmMap() 更改为 mMap 和系统:使用 try/catch 环绕。我更改它并将代码更改为。代码的构建没有问题,但在 map 中我没有看到要点。

来自
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();

enter image description here

Android Studio

最佳答案

确保您的 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 - 在 Android 应用程序中将 geojson 文件添加到我的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54107636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com