从 .csv 文件加载标记时如何获取标记的位置?
How to get a marker's position when marker is loaded from a .csv file?
我的应用程序中有一个地图片段,其中包含两种标记:一种是我通过事件 OnMapLongClick() 添加的,另一种是我从 .csv 文件添加的。我想通过在单击它们时启动新的 activity 来保存有关标记中每个坐标的一些详细信息。我遇到的问题是,对于从 .csv 文件添加的标记,它始终显示最后一个标记的位置。所以我的问题是,当我点击它时,如何获得来自 .csv 的相应标记的位置?这可以通过使用 Hashmap 更改 ArrayList 来实现吗?
MapsActivity(相关代码):
public void onMapLongClick(LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Introdus!");
markerOptions.draggable(true);
mMap.addMarker(markerOptions);
mMap.setOnMarkerClickListener(this);
latlngNew = latLng;
LatLng latlngNew;
}
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
openDialog();
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}
public void openDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(R.string.default_info_title);
dialog.show();
Button bt_da = (Button) dialog.findViewById(R.id.dialog_ok);
Button bt_nu = (Button) dialog.findViewById(R.id.dialog_cancel);
bt_da.setOnClickListener(this);
bt_nu.setOnClickListener(this);
bt_nu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
}
);
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
startActivity(intent);
}
});
}
public void Upload(View view) throws IOException {
File file = new File(getExternalFilesDir(null), "date.csv");
if (file.exists()) {
InputStream instream = new FileInputStream(file);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader = new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = " ";
while ((line = reader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(",")[0]);
double lon = Double.parseDouble(line.split(",")[1]);
latLngList.add(new LatLng(lat, lon));
mMap.setOnMarkerClickListener(this);
}
for (LatLng pos : latLngList) {
latlngNew=pos;
Marker m=mMap.addMarker(new MarkerOptions()
.position(pos)
.title("Din CSV!")
.draggable(true)
);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, DEFAULT_ZOOM));
}
}
else
{
Toast.makeText(MapsActivity.this,"Nu exista fisierul!", Toast.LENGTH_SHORT).show();
}
}
public void Sterge(View view){
mMap.clear();
}
}
那是因为您传递给意图 latlngNew
坐标:
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
由于循环指向最后一个标记:
for (LatLng pos : latLngList) {
latlngNew=pos;
...
}
因此,要传递给所选标记的意图坐标,您应该通过添加 LatLng
参数(如 openDialog(LatLng coords)
)修改 openDialog()
,并传递给所选标记的 openDialog(LatLng coords)
坐标。类似的东西:
public void openDialog(LatLng coords) {
//... your code without changes
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
// pass coords instead of latlngNew:
intent.putExtra("markerLat", coords.latitude);
intent.putExtra("markerLong", coords.longitude);
startActivity(intent);
}
});
}
和onMarkerClick()
改为
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
// pass marker coordinates here:
openDialog(marker.getPosition());
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}
我的应用程序中有一个地图片段,其中包含两种标记:一种是我通过事件 OnMapLongClick() 添加的,另一种是我从 .csv 文件添加的。我想通过在单击它们时启动新的 activity 来保存有关标记中每个坐标的一些详细信息。我遇到的问题是,对于从 .csv 文件添加的标记,它始终显示最后一个标记的位置。所以我的问题是,当我点击它时,如何获得来自 .csv 的相应标记的位置?这可以通过使用 Hashmap 更改 ArrayList 来实现吗?
MapsActivity(相关代码):
public void onMapLongClick(LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Introdus!");
markerOptions.draggable(true);
mMap.addMarker(markerOptions);
mMap.setOnMarkerClickListener(this);
latlngNew = latLng;
LatLng latlngNew;
}
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
openDialog();
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}
public void openDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(R.string.default_info_title);
dialog.show();
Button bt_da = (Button) dialog.findViewById(R.id.dialog_ok);
Button bt_nu = (Button) dialog.findViewById(R.id.dialog_cancel);
bt_da.setOnClickListener(this);
bt_nu.setOnClickListener(this);
bt_nu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
}
);
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
startActivity(intent);
}
});
}
public void Upload(View view) throws IOException {
File file = new File(getExternalFilesDir(null), "date.csv");
if (file.exists()) {
InputStream instream = new FileInputStream(file);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader = new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = " ";
while ((line = reader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(",")[0]);
double lon = Double.parseDouble(line.split(",")[1]);
latLngList.add(new LatLng(lat, lon));
mMap.setOnMarkerClickListener(this);
}
for (LatLng pos : latLngList) {
latlngNew=pos;
Marker m=mMap.addMarker(new MarkerOptions()
.position(pos)
.title("Din CSV!")
.draggable(true)
);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, DEFAULT_ZOOM));
}
}
else
{
Toast.makeText(MapsActivity.this,"Nu exista fisierul!", Toast.LENGTH_SHORT).show();
}
}
public void Sterge(View view){
mMap.clear();
}
}
那是因为您传递给意图 latlngNew
坐标:
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
由于循环指向最后一个标记:
for (LatLng pos : latLngList) {
latlngNew=pos;
...
}
因此,要传递给所选标记的意图坐标,您应该通过添加 LatLng
参数(如 openDialog(LatLng coords)
)修改 openDialog()
,并传递给所选标记的 openDialog(LatLng coords)
坐标。类似的东西:
public void openDialog(LatLng coords) {
//... your code without changes
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
// pass coords instead of latlngNew:
intent.putExtra("markerLat", coords.latitude);
intent.putExtra("markerLong", coords.longitude);
startActivity(intent);
}
});
}
和onMarkerClick()
改为
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
// pass marker coordinates here:
openDialog(marker.getPosition());
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}