javacc android系统开发一个定位系统

package com.mnc.location;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

/**
 * GPS�㲥
 * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> mnc
 */
public class GPSLocationService extends Service {
	
	private LocationManager locationManager;
	private Location location;
	private String provider;
	
	private double latitude ;
	private double longitude;
	
	private GetLocationHandler getLocationHandler;
	private UpdateLocationHandler updateLocationHandler;
	private LocationListener locationListener;
	private GPSLocationBinder gPSLocationBinder = new GPSLocationBinder();

	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria(); 
		//�����õĶ�λЧ�� 
		criteria.setAccuracy(Criteria.ACCURACY_FINE); 
		criteria.setAltitudeRequired(false); 
		criteria.setBearingRequired(false); 
		criteria.setCostAllowed(false);
		criteria.setPowerRequirement(Criteria.POWER_LOW); //ʹ��ʡ��ģʽ 
		provider = locationManager.getBestProvider(criteria, true); //��õ�ǰ��λ���ṩ�� 
		location = locationManager.getLastKnownLocation(provider);
		
		if(location==null) {
			getLocationHandler = new GetLocationHandler();
			new GetLocationThread().start(); 
		}
		
		//�����û����ı�
		updateLocationHandler = new UpdateLocationHandler();
		new UpdateLocationThread().start(); 
		return gPSLocationBinder;
	}
	
	/****���Location����***************************************************************/
	private class GetLocationHandler extends Handler {
		public void handleMessage(Message msg) {
			if(msg.obj != null) {
				location = (Location)msg.obj;
				latitude = location.getLatitude()* 1E6; // ����
				longitude = location.getLongitude()* 1E6; // �
//				double altitude = location.getAltitude(); // ����
				
				//����һ��intent  
				Intent intent = new Intent("GPSLocationService");
				intent.putExtra("latitude",latitude);
				intent.putExtra("longitude",longitude);
				sendBroadcast(intent);  //�㲥��ȥ  
			}
		}
	}
	private class GetLocationThread extends Thread{
		public void run(){
			while(location == null) location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			Message messagee = getLocationHandler.obtainMessage();
			messagee.obj = location;
			messagee.sendToTarget();
		}
	}
	/**********************************************************************************/
	
	/****����Location����ı�***************************************************************/
	private class UpdateLocationHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(msg.arg1 == 1 && msg.obj != null){
				latitude = location.getLatitude()* 1E6; // ����
				longitude = location.getLongitude()* 1E6; // �
//				double altitude = location.getAltitude(); // ����
				
				//����һ��intent  
				Intent intent = new Intent("GPSLocationService");
				intent.putExtra("latitude",latitude);
				intent.putExtra("longitude",longitude);
				sendBroadcast(intent);  //�㲥��ȥ  
			}	
		}
	}
	
	private class UpdateLocationThread extends Thread {
		public void run() {
			// TODO Auto-generated method stub
			locationListener = new LocationListener() {
				public void onLocationChanged(Location location) { // �����ı�ʱ�����˺������Provider������ͬ����꣬��Ͳ��ᱻ����
					if (location != null) {
						Log.i("SuperMap", "Location changed : Lat: "+ location.getLatitude() + " Lng: "+ location.getLongitude());
						locationManager.requestLocationUpdates((String)provider, 30000, 1, locationListener);   
						Message messagee = updateLocationHandler.obtainMessage();
						messagee.arg1 = 1;
						messagee.obj = location;
						messagee.sendToTarget();
					}
				}

				public void onProviderDisabled(String provider) {
					// Provider��disableʱ�����˺������GPS���ر�
				}
				public void onProviderEnabled(String provider) {
					// Provider��enableʱ�����˺������GPS����
				}

				public void onStatusChanged(String provider, int status,
						Bundle extras) {
					// Provider��ת̬�ڿ��á���ʱ�����ú��޷������״ֱ̬���л�ʱ�����˺���
				}
			};
		}
	}
	/**********************************************************************************/

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}
	
	private class GPSLocationBinder extends Binder{
		public double getLatitude() {
			return getLatitude();
		}

		public double getLongitude() {
			return getLongitude();
		}
	}
}

这个事gps定位的,写完了没有测试,以为项目对经度要求不是很高,后来用的基站定位,应该没啥问题,注释乱码了,呵呵

package com.mnc.location;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.widget.Toast;

public class GSMLocationService extends Service {

	private double latitude;
	private double longitude;

	private GSMLocationBinder gSMLocationBinder = new GSMLocationBinder();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub

		int cid = 0;
		int lac = 0;
		int mcc = 0;
		int mnc = 0;
		
		TelephonyManager mTelephonyManager = (TelephonyManager) this
				.getSystemService(Service.TELEPHONY_SERVICE);
		if (mTelephonyManager == null) {
//			ShowDebugInfo("mTelephonyManager == null");
			return null;
		}
		GsmCellLocation mGsmCellLocation = (GsmCellLocation) mTelephonyManager.getCellLocation();
		if (mGsmCellLocation == null) {
//			ShowDebugInfo("�޷����Gsm����");
			 cid = 7272;
			 lac = 28709;
		} else {
//			ShowDebugInfo("�����gms����");
			cid = mGsmCellLocation.getCid();
			lac = mGsmCellLocation.getLac();
			
			mcc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(0,3));
			mnc = Integer.valueOf(mTelephonyManager.getNetworkOperator().substring(3,5));
		}
		
		
		try {
			// ��װJSON��ѯ�ַ�
			JSONObject holder = new JSONObject();
			holder.put("version", "1.1.0");
			holder.put("host", "maps.google.com");
			// holder.put("address_language", "zh_CN");
			holder.put("request_address", true);
			JSONArray array = new JSONArray();
			JSONObject data = new JSONObject();
			data.put("cell_id", cid); // 25070
			data.put("location_area_code", lac);// 4474
			data.put("mobile_country_code", mcc);// 460
			data.put("mobile_network_code", mnc);// 0
			array.put(data);
			holder.put("cell_towers", array);
			// �������ӣ������������ܻ�Ӧ
			DefaultHttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://www.google.com/loc/json");
			StringEntity se = new StringEntity(holder.toString());
			post.setEntity(se);
			HttpResponse resp = client.execute(post);
			HttpEntity entity = resp.getEntity();
			BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
			StringBuffer sb = new StringBuffer();
			String result = br.readLine();
			while (result != null) {
				sb.append(result);
				result = br.readLine();
			}
			String message=sb.toString();
			Log.v("ddddd=====", message);
			JSONObject jsonObject = new JSONObject(message.toString()); 
			JSONObject location = jsonObject.getJSONObject("location");
			latitude = new Double(location.getString("latitude")).doubleValue();
			longitude = new Double(location.getString("longitude")).doubleValue();
			Log.v("fffff=====", latitude+"");
			Log.v("ggggg=====", longitude+"");
			
			//����һ��intent  
			intent = new Intent("GSMLocationService");
			intent.putExtra("latitude",latitude);
			intent.putExtra("longitude",longitude);
			sendBroadcast(intent);  //�㲥��ȥ  
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
//		String strCid = "http://www.anttna.com/cell2gps/cell2gps.php?lac="
//				+ lac + "&cellid=" + cid;
//		String strRS = GetCIDName(strCid);
//		Log.v("strCid", strCid);
//		Log.v("strRS", strRS);
//		String[] strSP = strRS.split(",");
//		if(strRS!=null && strRS.length()>0 && !"0".equals(strRS)){
//			Log.v("strSP[0]", strSP[0]);
//			Log.v("strSP[1]", strSP[1]);
//			latitude = new Double(strSP[0]).doubleValue() * 1E6;
//			longitude = new Double(strSP[1]).doubleValue() * 1E6;
//		}
//		Log.v("ddddd=====", latitude+"-"+longitude);
		
		
		

		// String strOutput = "CellID:" + cid + ", LAC:" + lac + "\n";
		// strOutput += "��վ��λ:\n����:";
		// strOutput += strSP[0];
		// strOutput += "\n";
		// strOutput += "�:";
		// strOutput += strSP[1];
		// strOutput += "\n";
		// String[] strSP2 = strSP[2].split(";");
		// strOutput += "ʡ��:";
		// strOutput += strSP2[0].substring(1);
		// strOutput += "\n";
		// strOutput += "����:";
		// strOutput += strSP2[1];
		// strOutput += "\n";
		// strOutput += "��:";
		// strOutput += strSP2[2];
		// strOutput += "\n";
		// strOutput += "�ֵ�:";
		// strOutput += strSP2[3];
		// strOutput += "\n";
		// strOutput += "�ܱ�:";
		// strOutput += strSP2[4];
		// strOutput += "\n";

		return gSMLocationBinder;
	}

	// ����IO����
	public String GetCIDName(String url) {
		URL internetUrl = null;
		String strRS = new String("0");
		int length = -1;

		try {
			internetUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
//			ShowDebugInfo("���������쳣,��ַ����");
			return strRS;
		}

		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) internetUrl.openConnection();
			conn.setDoInput(true);
			conn.setConnectTimeout(2000);
			byte[] data = new byte[4096];
			for (int i = 0; i < 4096; i++) {
				data[i] = 0;
			}
			conn.connect();

			InputStream is = conn.getInputStream();
			length = is.read(data);
			strRS = new String(data, "GBK");
		} catch (IOException e) {
			e.printStackTrace();
//			ShowDebugInfo(e.getMessage());
			// myTextView.setText(e.getMessage());
		} finally {
			if (conn != null) {
				conn.disconnect();
				conn = null;
				internetUrl = null;
			}
		}

		if (length == -1 || strRS.length() < 10) {
			return strRS;
		}

//		ShowDebugInfo("�����ȡ�ɹ�");
		return strRS.substring(0, length);
	}

	// �������
	public void ShowDebugInfo(String txt) {
		Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
	}

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	private class GSMLocationBinder extends Binder {
		public double getLatitude() {
			return getLatitude();
		}

		public double getLongitude() {
			return getLongitude();
		}
	}

}
基站定位的也发给你把,因为在公交车里的话有时候gps的信号肯定不好,这个基站定位我测试过,在我这误差大概个三五百米的样子,周边的基站数量应该对定位影响很大
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值