从文本文件中读取纬度和经度 [英] Reading latitude and longitude from a text file

查看:97
本文介绍了从文本文件中读取纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个硬件编码的经纬度和长仓。相反,我想从一个名为cities.text的文本文件中读取lat和long,其中包含以下内容:

$ p $ Dublin $ 53.347860 -6.272487
凯利52.264007 -9.686990
软木51.892171 -8.475068

可以使用一些帮助,因为我'找到这个功能很难实现,并想知道有人可以帮我完成这个工作吗?。

这是我到目前为止的代码。



地图活动

  public class MapActivity扩展AppCompatActivity实现的OnMapReadyCallback {

String city =;
MapFragment mf;
GoogleMap地图;

@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已加载,但尚未布局
this.map = map;
//在地图加载时运行代码

double lat,lon;

if(city.equals(Dublin))
{
lat = 53.347860;
lon = -6.272487;
}
else if(city.equals(Kerry))
{
lat = 52.264007;
lon = -9.686990;

$ b else if(city.equals(Cork))
{
lat = 51.892171;
lon = -8.475068;
}
else
{
lat = 0;
lon = 0;

$ b map.addMarker(new MarkerOptions()
.position(new LatLng(lat,lon))
.title()
);

map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lon)));
map.moveCamera(CameraUpdateFactory.zoomTo(10));


$ b @Override
public boolean onCreateOptionsMenu(菜单菜单){
//膨胀菜单;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.menu_map,menu);
返回true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here here。操作栏将
//自动处理Home / Up按钮上的点击,只要您在AndroidManifest.xml中指定了一个父活动,就可以长按
//。
int id = item.getItemId();

// noinspection SimplifiableIfStatement
if(id == R.id.action_settings){
return true;
}

返回super.onOptionsItemSelected(item);


CityListFragment

  public class CityListFragment extends Fragment {


$ b public CityListFragment(){
//必须为空public构造函数
}

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

$ b @覆盖
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled 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 $ b $ public void onItemClick(AdapterView<> list,View row,int index, long rowID){
//用户单击该项目时运行的代码
//启动新的具有孔详细信息的活动
((CityMap)getActivity())。setCity((String)list。 getItemAtPosition(index));
((CityMap)getActivity())。showMap();

}
}
);
返回视图;
}


}

CityMap p>

  public class CityMap extends AppCompatActivity implements OnMapReadyCallback {

String city =;
MapFragment mf;
GoogleMap地图;
双纬度;
双经度;

public void setCity(String city)
{
this.city = city;

$ b $ public void showMap()
{
mf =(MapFragment)getFragmentManager()。findFragmentById(R.id.the_map);
if(mf == null){
// CityMapFragment(Fragment B)不在布局中(手机布局),
//启动MapActivity(Activity B)
//并将它传递给选定项目的信息
Intent intent = new Intent(this,MapActivity.class);
intent.putExtra(city,city);
startActivity(intent);
} else {
// CityMApFragment(Fragment B)在布局中(平板布局)
mf.getMapAsync(this);



$ b @Override
public void onMapReady(GoogleMap map){// map已加载但尚未布局
this.map = map;
//在地图加载时运行代码

double lat,lon;
if(city.equals(myLocation))
{
lat = latitude;
lon = longitude;


$ b else if(city.equals(Dublin))
{
lat = 53.347860;
lon = -6.272487;
}
else if(city.equals(Kerry))
{
lat = 52.264007;
lon = -9.686990;


$ b else if(city.equals(Cork))
{
lat = 51.892171;
lon = -8.475068;
}
else
{
lat = 0;
lon = 0;

$ b map.addMarker(new MarkerOptions()
.position(new LatLng(lat,lon))
.title()
);

map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lon)));
map.moveCamera(CameraUpdateFactory.zoomTo(10));


$ b $ @覆盖
保护无效onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(菜单菜单){
//膨胀菜单;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.menu_city_map,menu);
返回true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here here。操作栏将
//自动处理Home / Up按钮上的点击,只要您在AndroidManifest.xml中指定了一个父活动,就可以长按
//。
int id = item.getItemId();

// noinspection SimplifiableIfStatement
if(id == R.id.action_settings){
return true;
}

返回super.onOptionsItemSelected(item);



$ div $解析方案假设你可以使用这个函数在你的assets文件夹中包含lat-lng数据,你可以使用这个函数

  private HashMap< ;字符串,位置> readFile(String path){
HashMap< String,Location> citiesLoactionMap = new HashMap<>();

String dump = null;
尝试{
dump = readFromAssets(mContext,path);
} catch(IOException e){
e.printStackTrace();

if(dump!= null&&!dump.isEmpty()){

String [] cities = dump.split(\\\
) ;
(String city:cities){

String [] cityData = city.split(\\s +); (cityData [0]!= null& cityData [1]!= null& cityData [2]!= null){

位置位置=新位置(cityData [0]);
location.setLatitude(Double.parseDouble(cityData [1]));
location.setLongitude(Double.parseDouble(cityData [2]));
citiesLoactionMap.put(cityData [0],location);

}

}

}
return citiesLoactionMap;
}

这里路径就是文件名,例如cities.txt


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);
}
}

解决方案

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"

这篇关于从文本文件中读取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆