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

标签 android google-maps-android-api-2 android-maps-utils

我对 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/

相关文章:

android - 使用集群时如何禁用android map 标记点击自动居中?

android - 如何修复 'Unable to determine application id: com.android.tools.idea.run.ApkProvisionException: No outputs for the main artifact of variant:'

java - IBM Watson 语音到文本集成

java - 多边形触摸检测 Google Map API V2

google-maps - 来自 android :android-maps-utils changed and is not retrocompatible 的 ClusterManager

onclicklistener - 在 Google Maps Android API 中通过 onclick 选择功能

android - 按钮上方带有文本的单选按钮

android - 将 Firebase 任务<Void> 包装到 RxJava CompletableEmitter 中?

java - Android - 具有固定半径(以米为单位)的 Google Maps API 热图

具有离线 map 和数据库的 Android 应用程序