无法解析符号 'GeoJsonLayer '
Cannot resolve symbol 'GeoJsonLayer '
我目前正在使用 Google 地图 API,我正在尝试将带有标记的 GEOJSON 文件作为图层连接到地图。
我从文档中获取并且我目前正在尝试使用的行是:
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.geoJsonFile,
getApplicationContext());
错误是Cannot resolve symbol 'GeoJsonLayer'
我已将编译 org.json:json:20090211
添加到我的依赖项中,但这并没有解决问题。
我正在使用 Android Studio 为 Android OS 编码。
有什么帮助吗?谢谢
编辑:
Class代码:
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
}
}
代码的注释部分有效,但会使应用程序崩溃,如果没有注释代码,地图可以正常工作并在我的设备上正常加载。
您需要在 build.gradle
文件中添加 google-maps-utils
。
dependencies {
implementation 'com.google.maps.android:android-maps-utils:x.y.z'
}
其中 x.y.z 是版本。最新的是:0.4
.
您可能还需要 google-maps
依赖项(和 play-services-base),并且可以添加到 build.gradle 文件中,如下所示:
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'
}
如果你想要 play-services 的所有库,你可以跳过这两个依赖并添加所有 play-services 依赖,像这样:
implementation 'com.google.android.gms:play-services:8.3.0'
请记住,这会将所有播放服务库添加到您的 APK 中,这可能会导致 64k 方法限制和更大的 APK 大小。
确保您已添加 Deividi Cavarzan 提到的依赖项
另外请确保您的 mapjson 文件格式正确
检查此 geojson 文件代码:
{
"type": "FeatureCollection",
"features": [{</p>
<pre><code> "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
}
}
]
}
要检查您的 GeoJson 文件是否正确,请访问:http://geojsonlint.com/
我目前正在使用 Google 地图 API,我正在尝试将带有标记的 GEOJSON 文件作为图层连接到地图。
我从文档中获取并且我目前正在尝试使用的行是:
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.geoJsonFile,
getApplicationContext());
错误是Cannot resolve symbol 'GeoJsonLayer'
我已将编译 org.json:json:20090211
添加到我的依赖项中,但这并没有解决问题。
我正在使用 Android Studio 为 Android OS 编码。
有什么帮助吗?谢谢
编辑:
Class代码:
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
}
}
代码的注释部分有效,但会使应用程序崩溃,如果没有注释代码,地图可以正常工作并在我的设备上正常加载。
您需要在 build.gradle
文件中添加 google-maps-utils
。
dependencies {
implementation 'com.google.maps.android:android-maps-utils:x.y.z'
}
其中 x.y.z 是版本。最新的是:0.4
.
您可能还需要 google-maps
依赖项(和 play-services-base),并且可以添加到 build.gradle 文件中,如下所示:
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'
}
如果你想要 play-services 的所有库,你可以跳过这两个依赖并添加所有 play-services 依赖,像这样:
implementation 'com.google.android.gms:play-services:8.3.0'
请记住,这会将所有播放服务库添加到您的 APK 中,这可能会导致 64k 方法限制和更大的 APK 大小。
确保您已添加 Deividi Cavarzan 提到的依赖项
另外请确保您的 mapjson 文件格式正确
检查此 geojson 文件代码:
{
"type": "FeatureCollection",
"features": [{</p>
<pre><code> "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
}
}
]
}
要检查您的 GeoJson 文件是否正确,请访问:http://geojsonlint.com/