Map and XML parsing

Map dependence

<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Map dependence

implementation ‘com.amap.api:location:4.2.0’
implementation’com.amap.api:map2d:5.2.0’

Map Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<com.amap.api.maps2d.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.amap.api.maps2d.MapView>

</LinearLayout>

Map of the main class

public class MainActivity extends AppCompatActivity implements AMapLocationListener {
private MapView mapView;
private AMap aMap;
private AMapLocationClient client;//负责定位
private AMapLocationClientOption clientOption;
Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        client.stopLocation();
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView= findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);//必须要写


    initMap();
}

private void initMap() {
    aMap=mapView.getMap();//获得地图对象
    aMap.setMapType(AMap.MAP_TYPE_SATELLITE);//地图切换(普通,卫星)

    client = new AMapLocationClient(this);
    client.setLocationListener(this);//给定位加监听

    clientOption = new AMapLocationClientOption();
    clientOption.setInterval(1000);//每隔1000去定位一次
    clientOption.setNeedAddress(true);//是否显示地址
    clientOption.setMockEnable(true);//是否显示模拟定位
    clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//高精度地位
    client.setLocationOption(clientOption);
    client.startLocation();//启动定位
//        client.stopLocation();//关闭定位

    handler.sendEmptyMessageDelayed(0,5000);//延长发送

}

/**
 * 定位发生改变。要获得当前的位置
 * @param aMapLocation
 */
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
    if (aMapLocation.getErrorCode()==0) {
        double lat = aMapLocation.getLatitude();//纬度
        double lon = aMapLocation.getLongitude();//经度
        LatLng latLng = new LatLng(lat, lon);//坐标对象
        aMap.clear();
        MarkerOptions options = new MarkerOptions();//小红点,定位标记
        options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        options.position(latLng);//图标要显示的位置
        options.draggable(true);

        aMap.addMarker(options);//给地图添加标记

        CameraUpdate update = CameraUpdateFactory.changeLatLng(latLng);//更新定位区域
        aMap.moveCamera(update);//显示到定位区域
    }else{
        //定位失败

    }
}

@Override//界面没有交点但可见
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override//界面
protected void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    mapView.onPause();
}
}

effect

Here Insert Picture Description

Xml parsing code

xml

<bookstore>
    <book id="1">
        <name>冰与火之歌</name>
        <author>乔治马丁</author>
        <year>2014</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>安徒生童话</name>
        <author>安徒生</author>
        <year>2004</year>
        <price>77</price>
    </book>
</bookstore>

Document Brief

/**
 * 简析为了获得,对象,集合
 * xml:标记语言,用来做网络传输
 */
public class MainActivity extends AppCompatActivity {

    List<BookBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dom();

    }

    private void dom() {
        DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();//获得工厂
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();//创建builder对象

        Log.e("###","###1");
        InputStream inputStream = getAssets().open("book.xml");
        Log.e("###","###2");
        Document document = builder.parse(inputStream);//把文件添加到内存中

        NodeList nodeList = document.getElementsByTagName("book");//获取文件中所有节点名为book的内容,并打算将这些内容存入到nodeList中
        //由于有好多个book节点,所以我们要对nodelist做遍历

        for (int i=0;i<nodeList.getLength();i++){
            //这样就拿到了其中一个book节点的内容,也就是一个对象的内容,包括5个属性
            Log.e("###","###3");
            Node node = nodeList.item(i);//当i等于0的时候,获得的是第一个book对象,在文件中一共有2个book对象
            BookBean bookBean =new BookBean();


            NodeList childNodes = node.getChildNodes();

            for (int j=0;j<childNodes.getLength();j++){
                Node item = childNodes.item(j);

                if ("book".equals(item.getNodeName())){
                    NamedNodeMap attributes = item.getAttributes();
                    Node id = attributes.getNamedItem("id");
                    Log.e("####",id+"");
                    bookBean.setId(id.getTextContent());
                }else if ("name".equals(item.getNodeName())){

                    bookBean.setName(item.getTextContent());
                }else if("author".equals(item.getNodeName())){
                    bookBean.setAuthor(item.getTextContent());
                }else if("year".equals(item.getNodeName())){
                    bookBean.setYear(item.getTextContent());
                }else if ("price".equals(item.getNodeName())){
                    bookBean.setPrice(item.getTextContent());
                }
            }

            list.add(bookBean);
            Log.e("###",list+"");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    }

Renderings

[BookBean {id = '1', name = 'A Song of Ice and Fire', author = 'George Martin', year = '2014', price = '89 '}, BookBean {id =' 2 ', name =' Hans Christian Andersen ', author =' Anderson ', year =' 2004 ', price = '77'}]

pull parsing code

public class Main2Activity extends AppCompatActivity {

List<BookBean> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        XmlPullParser parser = factory.newPullParser();

        InputStream open = getAssets().open("book.xml");
        parser.setInput(open,"utf_8");

        int eventType = parser.getEventType();
        BookBean bookBean=null;
        while (eventType!=XmlPullParser.END_DOCUMENT){

            switch (eventType){
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("book")){
                        bookBean = new BookBean();
                        String attributeValue = parser.getAttributeValue(0);
                        bookBean.setId(attributeValue);
                    }else if (parser.getName().equals("name")){
                        bookBean.setName(parser.nextText());
                    }else if (parser.getName().equals("author")){
                        bookBean.setAuthor(parser.nextText());
                    }else if (parser.getName().equals("year")){
                        bookBean.setYear(parser.nextText());
                    }else if (parser.getName().equals("price")){
                        bookBean.setPrice(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("book")){
                        list.add(bookBean);
                    }
                    break;
            }
            eventType = parser.next();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("####",list+"");
}
}

Renderings

[BookBean {id = '1', name = 'A Song of Ice and Fire', author = 'George Martin', year = '2014', price = '89 '}, BookBean {id =' 2 ', name =' Hans Christian Andersen ', author =' Anderson ', year =' 2004 ', price = '77'}]

Guess you like

Origin blog.csdn.net/wangwei_weibo/article/details/93136154