[英]Cannot resolve symbol 'GeoJsonLayer '
我目前正在使用Google Maps API,並且嘗試將帶有標記的GEOJSON文件作為圖層連接到地圖。
我從文檔中摘錄的行現在正在嘗試使用:
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.geoJsonFile,
getApplicationContext());
錯誤是Cannot resolve symbol 'GeoJsonLayer'
我已經將編譯org.json:json:20090211
添加到我的依賴項中,但這並沒有解決問題。
我正在使用Android Studio為Android OS編碼。
有什么幫助嗎? 謝謝
編輯:
班級代碼:
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'
}
其中xyz是版本。 最新的是: 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": [{
"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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.