我对 Android Studio 和 Java 的任何事情都很陌生。我尝试制作一个带有一些兴趣点的地图应用程序。这些点是一个geojson文件。我阅读了 google 的示例及其网站(Google Maps Android GeoJSON Utility)中的说明,但我的代码中有一些错误。第一个错误与 getmMap 方法有关,(错误:找不到符号方法 getnMap())。我找到解决方案的第二个是关于文件它不需要扩展名(geojson)。我在 rs 中创建了一个名为 raw 的文件夹,并在其中添加了 geojson 文件。
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();
}
}
错误:cnnot find 符号方法 getmMap()
我将 getmMap() 更改为 mMap 和系统:使用 try/catch 环绕。我更改它并将代码更改为。代码的构建没有问题,但在地图中我没有看到要点。
从
GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.nbg_bank,
getApplicationContext());
layer.addLayerToMap();
至
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();