我正在尝试添加GeoJson文件。我使用的是Google https://developers.google.com/maps/documentation/android-sdk/utility/geojson的代码示例 但是我收到以下错误。
getMap()
无法解析方法“ getMap()”
addLayerToMap()
无法解析符号“ addLayerToMap”
我读到我必须用getmapAsync更改getmap,但是它什么都没有改变。
package com.example.testnavbar;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.data.geojson.GeoJsonFeature;
import com.google.maps.android.data.geojson.GeoJsonLayer;
import com.google.maps.android.data.geojson.GeoJsonLineString;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.List;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class HomeFragment extends Fragment implements OnMapReadyCallback {
private final static String mLogTag = "GeoJson";
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate( R.layout.fragment_home, container, false );
return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated( view, savedInstanceState );
mMapView = (MapView) mView.findViewById( R.id.map );
if (mMapView != null){
mMapView.onCreate( null );
mMapView.onResume();
mMapView.getMapAsync( this );
}
}
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.picnicsites,
getApplicationContext());
layer.addLayerToMap();
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType( GoogleMap.MAP_TYPE_SATELLITE );
googleMap.addMarker( new MarkerOptions()
.position(new LatLng( 35.126411, 33.429859 ) )
.title( "Cyprus" )
.snippet( "Center of island" )
.icon( BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
CameraPosition Liberty = CameraPosition.builder()
.target(new LatLng( 36.126411, 33.429859 ))
.zoom( 8f )
.bearing( 0 )
.tilt( 0 )
.build();
googleMap.moveCamera( CameraUpdateFactory.newCameraPosition( Liberty ) );
}
}
答案 0 :(得分:1)
似乎是各行的陈述:
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.picnicsites,
getApplicationContext());
layer.addLayerToMap();
是错误的-它们应该位于onMapReady(GoogleMap googleMap)
内部(新的GeoJsonLayer(getMap()...
应该更改为GeoJsonLayer(mGoogleMap...
),就像这样:
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
// move them here:
GeoJsonLayer layer = new GeoJsonLayer(mGoogleMap, R.raw.picnicsites,
getApplicationContext());
layer.addLayerToMap();
googleMap.setMapType( GoogleMap.MAP_TYPE_SATELLITE );
googleMap.addMarker( new MarkerOptions()
.position(new LatLng( 35.126411, 33.429859 ) )
.title( "Cyprus" )
.snippet( "Center of island" )
.icon( BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
CameraPosition Liberty = CameraPosition.builder()
.target(new LatLng( 36.126411, 33.429859 ))
.zoom( 8f )
.bearing( 0 )
.tilt( 0 )
.build();
googleMap.moveCamera( CameraUpdateFactory.newCameraPosition( Liberty ) );
}
并删除
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
行。