Android入门总结-基于位置的服务

最近看了一下Android,只看书不总结一下,总感觉东西并没有放在脑子里,人过留名,雁过留声,遂写下这篇总结。

书本看的是《Android编程兵书》,但这篇总结的大纲是按照《第一行代码》的章节目录撰写的。两本书的内容结构大致相同,Android入门的话只看其中一本就够了。

基于位置的服务简称LBS

Android自身提供的GeoCoder存在Bug并且有一定的概率无法解析出位置的信息。Google提供了一套Geocoding API来替代GeoCoder。
Geocoding的官方文档

对经纬度进行解析示例代码

public class MainActivity extends Activity{
    public static final int SHOW_LOCATION = 0;

    private void showLocation(final location){
        new Thread(new Runnable(){
            public void run(){
                try{
                    StringBuilder url = new StringBuilder();
                    url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
                    url.append(location.getLatitude()).append(",");
                    url.append(location.getLongitude());
                    url.append("&sensor=false");

                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url.toString());

                    httpGet.addHeader("Accept-Language", "zh-CN");
                    HttpResponse httpResponse - httpClient.execute(httpGet);
                    if(httpRespons.getStatusLine().getStatusCode() == 200){
                        HttpEntity entity = httpRespons.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");
                        JSONObject jsonObject = new JSONObject(response);

                        JSONArray resultArray = jsonObject.getJSONArray("results");

                        if(resultArray.length() > 0){
                            JSONObject subObject = resultArray.getJSONObject(0);
                            String address = subObject.getString("formatted_address");
                            Message message = new Message();
                            message.what =  SHOW_LOCATION;
                            message.obj = address;
                            handler.sendMessage(message);
                        }
                    }
                }catch(Exeception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch(msg.what){
            case SHOW_LOCATION:
                String currentPosition = (String) msg.obj;
                positionTextView.setText(currentPosition);
                break;
            default:
                break;
            }
        }
    }
}

百度地图的使用
定位到我的位置

public void navigateTo(Location location){
    MapController controller = mapView.getController();
    controller.setZoom(16); // 设置缩放级别
    GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1e6), (int)(location.getLongitude() * 1e6));
    controller.setCenter(point);
}

//  使用MyLocationOverlay和PopupOverlay等覆盖物使界面更加丰富。

//  MyLocationOverlay
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(mapview);
LocationData locationData = new LocationData();
locationData.latitude = location.getLatitude();
locationData.longitude = location.getLongitude();

myLocationOverlay.setData(locationData);
mapView.getOverlays().add(myLocationOverlay);
mapView.refresh();  // 刷新使新增覆盖物生效


//  PopupOverlay
PopupOverlay pop = new PopupOverlay(mapView, new PopupClickListener(){
    public void onClickedPopup(int index){
        Toast.makeText(MainActivity.this, "you clicked button " + index, Toast.LENGHT_SHORT).show();
    }
});

// 长度为3的bitmap数组
Bitmap[] bitmaps = new Bitmap[3];
try{
    bitmaps[0] = BitmapFactory.decodeResource(getResources(), R.drawable.left);
    bitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.middle);
    bitmaps[2] = BitmapFactory.decodeResource(getResources(), R.drawable.right);
}catch(Exception e){
    e.printStackTrace();
}
// 第三个参数为偏移距离
pop.showPopup(bitmaps, point, 18);

git分支的用法

git branch -D branchname    //删除分支
git push origin master  //  
pull 命令相当于将fetch和merge两个命令放在一起执行

你可能感兴趣的:(Android入门总结-基于位置的服务)