简体   繁体   中英

Cannot resolve symbol 'GeoJsonLayer '

I am currently using the Google Maps API and I am attempting to connect a GEOJSON file with markers as a layer to the map.

The line I have taken from the documentation and I am currently trying to use is:

GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.geoJsonFile,
        getApplicationContext());

The error is Cannot resolve symbol 'GeoJsonLayer'

I have added compile org.json:json:20090211 to my dependencies but this hasn't solved the problem.

I am coding for Android OS using Android Studio.

Any help please? Thanks

EDIT:

Class code:

package com.example.macbookair........;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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.geojson.GeoJsonLayer;

import org.json.JSONException;

import java.io.IOException;

public class Maps extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private GeoJsonLayer layer1;

    @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);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


      /*  MapFragment mapFrag = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        try {
            GeoJsonLayer layer1 = new GeoJsonLayer(mapFrag.getMap(), R.raw.mapjson,
                    getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        layer1.addLayerToMap(); */
        // Add a marker in Sydney and move the camera
    }
}

The commented part of the code works but makes the application crash, without the commented code the map works fine and loads fine on my device.

You need to add the google-maps-utils in your build.gradle file.

dependencies {
    implementation 'com.google.maps.android:android-maps-utils:x.y.z'
}

Where xyz is the version. The latest one is: 0.4 .

You can also need the google-maps dependency (and play-services-base), and can be added in build.gradle file as listed:

dependencies {
    implementation 'com.google.maps.android:android-maps-utils:x.y.z'
    implementation 'com.google.android.gms:play-services-maps:8.3.0'
    implementation 'com.google.android.gms:play-services-base:8.3.0'
}

If you want all the libraries from play-services, you can skip these two dependencies and add all play-services dependencies, like this:

implementation 'com.google.android.gms:play-services:8.3.0'

Remember that this will add all play-services libs into your APK, and this may cause a 64k method limit and bigger APK size.

Reference to install play-services

Reference to Google Maps Utils

Make sure you have added dependencies mentioned by Deividi Cavarzan

Plus make sure your mapjson 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
           }
    }
]

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM