当前位置: 代码迷 >> Android >> 【android】运用百度api获得你的地址
  详细解决方案

【android】运用百度api获得你的地址

热度:7   发布时间:2016-05-01 19:28:17.0
【android】使用百度api获得你的地址
采用百度开放平台的api对自己位置的定位的好处有很多。
1,完全不需要你做任何事情。程序自动给调用。
2,稳定。流量少。之前做的有些程序是从网络上抓取自己的ip地址来进行定位。
而百度的这个,返回一些你位置的基本信息。
一下就是代码:
基础类,这个类保存个人位置信息。
package com.duduli.li;public class Addr {	private String time;	private String error;	private String pointX;	private String PointY;	private String radius;	private String addr;	public String getTime() {		return time;	}	public void setTime(String time) {		this.time = time;	}	public String getPointX() {		return pointX;	}	public void setPointX(String pointX) {		this.pointX = pointX;	}	public String getPointY() {		return PointY;	}	public void setPointY(String pointY) {		PointY = pointY;	}	public String getError() {		return error;	}	public void setError(String error) {		this.error = error;	}	public String getRadius() {		return radius;	}	public void setRadius(String radius) {		this.radius = radius;	}	public String getAddr() {		return addr;	}	public void setAddr(String addr) {		this.addr = addr;	}}


第二个为百度返回json的解析类,将获得的信息解析到基础类中进行保存。
package com.duduli.li;import org.json.JSONException;import org.json.JSONObject;public class JsonParse {		public Addr parseJson(String json) throws JSONException{				Addr addrMsg = new Addr();		JSONObject jo = new JSONObject(json);//		解析json中的result值		String result = jo.getString("result");		JSONObject resultJO = new JSONObject(result);		String time = resultJO.getString("time");		String error = resultJO.getString("error");		addrMsg.setTime(time);		addrMsg.setError(error);		//		解析json中的content值		String content = jo.getString("content");		JSONObject contentJO = new JSONObject(content);		String point = contentJO.getString("point");		//		解析json中point数据		JSONObject pointJO = new JSONObject(point);		String x = pointJO.getString("x");		String y = pointJO.getString("y");		addrMsg.setPointX(x);		addrMsg.setPointY(y);						String radius = contentJO.getString("radius");		addrMsg.setRadius(radius);				//		解析json中add数据		String addr = contentJO.getString("addr");		JSONObject addrJO = new JSONObject(addr);				String detail = addrJO.getString("detail");		addrMsg.setAddr(detail);				return addrMsg;	}}


第三个为layout的main.xml
    <com.baidu.mapapi.MapView android:id="@+id/bmapsView"		android:layout_width="fill_parent" android:layout_height="fill_parent"		android:clickable="true" />


最后是activity。可以显示地图和定位的。
package com.duduli.li;import org.json.JSONException;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.os.Bundle;import com.baidu.location.LocServiceMode;import com.baidu.location.LocationChangedListener;import com.baidu.location.LocationClient;import com.baidu.location.ReceiveListener;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;public class BaiduMapTestActivity extends MapActivity {	private LocationClient lc ;	private BMapManager mBMapMan;	private MapView mMapView;	private double x;	private double y;//	private GeoPoint gp;		@Override	protected void onCreate(Bundle arg0) {		// TODO Auto-generated method stub		super.onCreate(arg0);		setContentView(R.layout.main);				mBMapMan = new BMapManager(getApplication());		mBMapMan.init("279B8BABF0AA0A98B33A494C9E310BBB10137789", null);		super.initMapActivity(mBMapMan);				lc = new LocationClient(this);        lc.setCoorType("gcj02");        lc.setServiceMode(LocServiceMode.Background);        lc.addLocationChangedlistener(new MyLocationChangedListener());        lc.addRecerveListener(new MyReceiveListenner());        lc.start();                try {			Thread.sleep(3000);		} catch (InterruptedException e1) {			// TODO Auto-generated catch block			e1.printStackTrace();		}		lc.getLocation();	}	@Override	protected boolean isRouteDisplayed() {		// TODO Auto-generated method stub		return false;	}			public class MyLocationChangedListener implements LocationChangedListener {        public void onLocationChanged() {            //在此添加,位置改变触发的功能。//        	tv.append(text)        }    }            public class MyReceiveListenner implements ReceiveListener{        public void onReceive(String strData) {            //在此处理 获取到的定位结果的json串。        	            try {    			JsonParse jp = new JsonParse();    			Addr ad = jp.parseJson(strData);//    			x = getPoint(ad.getPointX());//114.105//    			y = getPoint(ad.getPointY());//22.54    			    			x = Double.valueOf(ad.getPointX());    			y = Double.valueOf(ad.getPointY());    			mMapView = (MapView) findViewById(R.id.bmapsView);    			    			mMapView.setBuiltInZoomControls(true);    			mMapView.getOverlays().add(new MyOverlay());    			    			MapController mc  = mMapView.getController();    			    			GeoPoint gp = new GeoPoint((int)(y*1E6),(int)(x*1E6));//    			GeoPoint gp = new GeoPoint((int)(22.54762429*1E6),(int)(114.1054699*1E6));    			mc.setCenter(gp);    			mc.setZoom(12);    			    			    			mBMapMan.start();    			    		} catch (JSONException e) {    			// TODO Auto-generated catch block    			e.printStackTrace();    		}        }               /* public int getPoint(String s){        	String str = s.substring(0, s.indexOf("."))+s.substring(s.indexOf(".")+1, s.indexOf(".")+7);        	return Integer.valueOf(str);        }*/            }            public class MyOverlay extends Overlay {        GeoPoint geoPoint = new GeoPoint((int)(y*1E6),(int)(x*1E6));        Paint paint = new Paint();             @Override        public void draw(Canvas canvas, MapView mapView, boolean shadow) {                        Point point = mMapView.getProjection().toPixels(geoPoint, null);            Bitmap bmp = BitmapFactory.decodeResource(getResources(),android.R.drawable.presence_online);            canvas.drawBitmap(bmp,point.x, point.y,paint);            canvas.drawText("你在这里", point.x, point.y, paint);        }    }}


最后还有权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>	<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>	<uses-permission android:name="android.permission.INTERNET"></uses-permission>	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>	
  相关解决方案