MainActivity.class:
package com.example.map_one_dinwei; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.example.vo.Item; import com.example.vo.Root; import com.thoughtworks.xstream.XStream; public class MainActivity extends Activity { public MapView mapView = null; public BaiduMap baiduMap = null; // 定位相关声明 public LocationClient locationClient = null; //自定义图标 BitmapDescriptor mCurrentMarker = null; boolean isFirstLoc = true;// 是否首次定位 private TextView tt; private String URL = "http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml"; private ListView list_view; private List<Item> list; private TextView textView2; public BDLocationListener myListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mapView == null) return; //吐司 // Toast.makeText(MainActivity.this, "经度"+arg0.getLongitude()+"纬度"+arg0.getLatitude()+"地址"+arg0.getAddrStr()+"街道"+arg0.getStreet()+"时间"+arg0.getTime(), 0).show(); tt.setText(location.getAddrStr( )); MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); baiduMap.setMyLocationData(locData); //设置定位数据 if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16); //设置地图中心点以及缩放级别 // MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(u); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在使用SDK各组件之前初始化context信息,传入ApplicationContext // 注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); tt=(TextView) findViewById(R.id.tt); list_view=(ListView) findViewById(R.id.list_view); mapView = (MapView) this.findViewById(R.id.mapView); // 获取地图控件引用 baiduMap = mapView.getMap(); //开启定位图层 baiduMap.setMyLocationEnabled(true); locationClient = new LocationClient(getApplicationContext()); // 实例化LocationClient类 locationClient.registerLocationListener(myListener); // 注册监听函数 this.setLocationOption(); //设置定位参数 locationClient.start(); // 开始定位 new Thread(){ public void run() { new Asyn().execute(URL); }; }.start(); list_view.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent it=new Intent(getApplicationContext(),ThreeActivity.class); startActivity(it); } }); } private class Asyn extends AsyncTask<String, Integer, String>{ private String s; protected String doInBackground(String... params) { // TODO Auto-generated method stub s=Networkutil.httpol(URL); return s; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub XStream xStream=new XStream(); xStream.processAnnotations(Root.class); Root root=(Root) xStream.fromXML(result); List<Item> list=root.getResult().getItem(); Mybase base=new Mybase(list, getApplicationContext()); list_view.setAdapter(base); } } // 三个状态实现地图生命周期管理 @Override protected void onDestroy() { //退出时销毁定位 locationClient.stop(); baiduMap.setMyLocationEnabled(false); // TODO Auto-generated method stub super.onDestroy(); mapView.onDestroy(); mapView = null; } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mapView.onResume(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mapView.onPause(); } /** * 设置定位参数 */ private void setLocationOption() { LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开GPS option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式 option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true); // 返回的定位结果包含地址信息 option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向 locationClient.setLocOption(option); } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <com.baidu.mapapi.map.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:visibility="invisible" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- 添加地图控件 --> <TextView android:id="@+id/tt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dddddddddd" /> <ListView android:id="@+id/list_view" android:layout_width="wrap_content" android:layout_height="wrap_content" ></ListView> </LinearLayout> </FrameLayout> </RelativeLayout>
ThreeActivity.class:
package com.example.map_one_dinwei; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import com.example.vo.DataDemo; import com.example.vo.Demo; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshGridView; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.GridView; public class ThreeActivity extends Activity{ private PullToRefreshGridView grid_view; private List<Demo> data=new ArrayList<Demo>(); private int p=10; private int z=10; private Mybasetwo base; private String str; private String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="; private int i=1; private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: base=new Mybasetwo(data, getApplicationContext()); grid_view.setAdapter(base); break; case 3: base=new Mybasetwo(data, getApplicationContext()); grid_view.setAdapter(base); base.notifyDataSetChanged(); grid_view.onRefreshComplete(); break; case 4: base=new Mybasetwo(data, getApplicationContext()); grid_view.setAdapter(base); grid_view.onRefreshComplete(); break; default: break; } }; }; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.grid); grid_view=(PullToRefreshGridView) findViewById(R.id.grid_view); grid_view.setMode(Mode.BOTH); init(); grid_view.setOnRefreshListener(new OnRefreshListener<GridView>() { @Override public void onRefresh(PullToRefreshBase<GridView> refreshView) { // TODO Auto-generated method stub i++; new Thread(){ public void run() { url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i; getClient(); Gson g=new Gson(); DataDemo dd=g.fromJson(str, DataDemo.class); data=dd.getResult().getData(); handler.sendEmptyMessage(4); }; }.start(); } public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { // TODO Auto-generated method stub i++; new Thread(){ public void run() { url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i; getClient(); Gson g=new Gson(); DataDemo dd=g.fromJson(str, DataDemo.class); List<Demo> list=dd.getResult().getData(); data.addAll(list); handler.sendEmptyMessage(3); }; }.start(); } }); } private void init() { // TODO Auto-generated method stub new Thread(){ public void run() { url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn=10"; getClient(); Gson g=new Gson(); DataDemo dd=g.fromJson(str, DataDemo.class); data=dd.getResult().getData(); handler.sendEmptyMessage(0); }; }.start(); } private void getClient() { // TODO Auto-generated method stub HttpClient client=new DefaultHttpClient(); HttpGet get=new HttpGet(url); try { HttpResponse response=client.execute(get); StatusLine statusLine=response.getStatusLine(); int statusCode=statusLine.getStatusCode(); if(statusCode==200){ HttpEntity entity=response.getEntity(); str=EntityUtils.toString(entity,"utf-8"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }
grid.vml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.handmark.pulltorefresh.library.PullToRefreshGridView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" ptr:ptrDrawable="@drawable/ic_launcher" ptr:ptrMode="both" /> </LinearLayout>
NetWorkUtil.class:
package com.example.map_one_dinwei; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import android.os.DeadObjectException; public class Networkutil { public static String httpol(String url){ String result=""; HttpGet httpGet=new HttpGet(url); //设置请求参数 HttpParams parmars=new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(parmars, 5000); // HttpConnectionParams.setSoTimeout(parmars, 5*100); HttpClient httpClient=new DefaultHttpClient(parmars); try { HttpResponse httpResponse=httpClient.execute(httpGet); int len=httpResponse.getStatusLine().getStatusCode(); if(len==200){ HttpEntity httpEntity=httpResponse.getEntity(); result=EntityUtils.toString(httpEntity, "utf-8"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
解析数据参照上面发的两个随笔: