Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a geojson file to my Map in Android App

Tags:

I'm very new to anything about Android studio and Java. I try to make an map App with some points of interest. The points is a geojson file. I read the example of google and the explanation in their web site (Google Maps Android GeoJSON Utility) but I have some errors in my code.The first error is about getmMap method, (error:cannot find symbol method getnMap()). The second which I find a solution is about the file It didn't need the extension (geojson). I make a folder in rs with name raw and add the geojson file inside.

Full code of 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()

I change getmMap() to mMap and the system :Surround with try/catch. I change it and the code change to. The building of the code finish without problems but in map I didn't see the points.

From

GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.nbg_bank,
                getApplicationContext());

        layer.addLayerToMap();

To

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

like image 589
Vassilis Avatar asked Jan 09 '19 10:01

Vassilis


1 Answers

Make sure your json file format is correct.

Check for this geojson file code :

{
    "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
               }
        }
    ]
}

For more info refer : https://cran.r-project.org/web/packages/geojsonR/vignettes/the_geojsonR_package.html

To check whether your GeoJson file is correct or not visit : http://geojsonlint.com/

like image 121
Lakhani Aliraza Avatar answered Nov 11 '22 09:11

Lakhani Aliraza