我刚刚在我的Android应用上创建了地图。我在excel文件中有地址列表,我将其保存为CSV文件,将其放入res>生的。我已经在MapActivity中阅读了CSV。问题是,当我运行(应用程序工作正常)时,它只显示我当前位置的标记,但不显示我的地址列表的标记。我想做什么?我错过了什么吗?我的CSV文件读错了吗? Hlp请...希望你理解我。
这是我的Activity
中的CSVfile类public class CSVFile {
InputStream inputStream;
ArrayList<String[]> addressList = new ArrayList<>();
public CSVFile(InputStream is) {
this.inputStream = is;
}
public ArrayList<String[]> read() {
addressList = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
addressList.add(row);
}
} catch (IOException e) {
Log.e("Main", e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Main", e.getMessage());
}
}
return addressList;
}
}
我在OnCreate方法中调用该类。这是我的活动
public class ShowAllMap extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener, OnMapReadyCallback {
public static final String TAG = MapsActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private InputStream inputStream;
private CSVFile csvFile;
ArrayList<String[]> hold;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_all_map);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1);
inputStream = getResources().openRawResource(R.raw.allmap);
csvFile = new CSVFile(inputStream);
hold = csvFile.read();
if (hold != null) {
for (int x = 0; x > this.hold.size(); x++) {
try {
Geocoder gc = new Geocoder(ShowAllMap.this);
List<Address> list = gc.getFromLocationName(hold.get(x).toString(), 0);
if (list.size() > 0) {
Address add = list.get(0);
double lat = add.getLatitude();
double lng = add.getLongitude();
LatLng latLng = new LatLng(lat, lng);
MarkerOptions options = new MarkerOptions()
.position(latLng);
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.addMarker(options);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
setUpMapIfNeeded();//my fragment(R.id.maps)
}
.
.
.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
}
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
} else {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Toast.makeText(ShowAllMap.this, "Turn ON Your Location", Toast.LENGTH_LONG).show();
} else {
onLocationChanged(location);
}
}
}
此方法是我将标记放在当前位置的位置。
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
CameraPosition position = CameraPosition.builder()
.target(latLng)
.zoom(13f)
.bearing(0.0f)
.tilt(0.0f)
.build();
MarkerOptions options = new MarkerOptions()
.title("I'm Here")
.position(latLng);
mMap.addMarker(options);
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(position), null);
}