我目前有一个使用lat和long仓位进行硬编码的应用程序。相反,我想从名为cities.text的文本文件中读取lat和long,其中包含以下内容
Dublin 53.347860 -6.272487
Kerry 52.264007 -9.686990
Cork 51.892171 -8.475068
可以使用一些帮助,因为我发现这个功能难以实现,并且想知道有人可以帮我解决这个问题吗?
这是我到目前为止的代码。
地图活动
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
String city = "";
MapFragment mf;
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Intent I = getIntent();
city = I.getStringExtra("city");
mf = (MapFragment) getFragmentManager().findFragmentById(R.id.the_map);
mf.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) { // map is loaded but not laid out yet
this.map = map;
// code to run when the map has loaded
double lat, lon;
if (city.equals("Dublin"))
{
lat = 53.347860;
lon = -6.272487;
}
else if (city.equals("Kerry"))
{
lat= 52.264007;
lon= -9.686990;
}
else if (city.equals("Cork"))
{
lat= 51.892171;
lon= -8.475068;
}
else
{
lat=0;
lon=0;
}
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.title("")
);
map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));
map.moveCamera(CameraUpdateFactory.zoomTo(10));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CityListFragment
public class CityListFragment extends Fragment {
public CityListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_city_list, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_list_item_1, Arrays.asList("myLocation","Dublin","Kerry","Cork"));
ListView list = (ListView) view.findViewById(R.id.listView);
list.setAdapter(adapter);
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list, View row, int index, long rowID) {
// code to run when user clicks that item
// launch new Activity with holes details
((CityMap)getActivity()).setCity((String)list.getItemAtPosition(index));
((CityMap)getActivity()).showMap();
}
}
);
return view;
}
}
cityMap中
public class CityMap extends AppCompatActivity implements OnMapReadyCallback {
String city = "";
MapFragment mf;
GoogleMap map;
double latitude;
double longitude;
public void setCity(String city)
{
this.city = city;
}
public void showMap()
{
mf = (MapFragment) getFragmentManager().findFragmentById(R.id.the_map);
if (mf == null) {
// CityMapFragment (Fragment B) is not in the layout (handset layout),
// so start MapActivity (Activity B)
// and pass it the info about the selected item
Intent intent = new Intent(this, MapActivity.class);
intent.putExtra("city", city);
startActivity(intent);
} else {
// CityMApFragment (Fragment B) is in the layout (tablet layout)
mf.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap map) { // map is loaded but not laid out yet
this.map = map;
// code to run when the map has loaded
double lat, lon;
if (city.equals("myLocation"))
{
lat= latitude;
lon= longitude;
}
else if (city.equals("Dublin"))
{
lat = 53.347860;
lon = -6.272487;
}
else if (city.equals("Kerry"))
{
lat= 52.264007;
lon= -9.686990;
}
else if (city.equals("Cork"))
{
lat= 51.892171;
lon= -8.475068;
}
else
{
lat=0;
lon=0;
}
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.title("")
);
map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));
map.moveCamera(CameraUpdateFactory.zoomTo(10));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_city_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}