问题
I currently have an application hard coded with lat and long positions. Instead I want to read the lat and long from a text file called cities.text which contains the following
Dublin 53.347860 -6.272487
Kerry 52.264007 -9.686990
Cork 51.892171 -8.475068
Could use some help as I'm finding this feature difficult to implement and was wondering could someone help me with how this is done?.
Here's the code I have so far.
Map Activity
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);
}
}
回答1:
Assuming you have this file in your assets folder with the lat-lng data in the above format, you can use this function
private HashMap<String, Location> readFile(String path){
HashMap<String, Location> citiesLoactionMap = new HashMap<>();
String dump = null;
try {
dump = readFromAssets(mContext, path);
} catch (IOException e) {
e.printStackTrace();
}
if (dump != null && !dump.isEmpty()) {
String[] cities = dump.split("\n");
for(String city : cities){
String[] cityData = city.split("\\s+");
if(cityData[0] != null && cityData[1] != null && cityData[2] != null){
Location location = new Location(cityData[0]);
location.setLatitude(Double.parseDouble(cityData[1]));
location.setLongitude(Double.parseDouble(cityData[2]));
citiesLoactionMap.put(cityData[0], location);
}
}
}
return citiesLoactionMap;
}
Here path would be the the file name, e.g. "cities.txt"
来源:https://stackoverflow.com/questions/47509681/reading-latitude-and-longitude-from-a-text-file