---
title: "使用Cobub LBS位置服务获取当前位置信息"
author: "guowee"
date: "2010-08-09T17:08:58Z"
tags: []
categories: ["移动开发"]
slug: "cobub-lbs"
draft: false
lastmod: "2010-08-16T09:07:56Z"
id: 733130
url: "https://guowee.iteye.com/blog/733130"
---

**使用Cobub LBS位置服务获取当前位置信息**

\----------------------------------------------------------------------------------------------------------------------------------

目前网络上存在好多根据GPS，WIFI，CELL ID等获取当前位置信息的代码。Cobub LBS服务整合了几种定位方式,方便开发者使用。

 

主要提供了如下查询

 

根据GPS查询位置信息

根据基站信息查询位置

根据IP查询位置

根据位置查询GPS信息

根据WIFI查询位置信息

 

Cobub官方还提供了Andoid版本的源代码下载。可以从以下地址Checkout。

[https://cobub.svn.codeplex.com/svn/trunk/](https://cobub.svn.codeplex.com/svn/trunk/)

 

此外官方还提供了网上查询版本，网址为[www.cobub.com](http://www.cobub.com)

开发者指南

[http://www.cobub.com/lbs/?developer](http://www.cobub.com/lbs/?developer)

Web Service 地址

[http://www.cobub.com/lbs/index.php?lbservice](http://www.cobub.com/lbs/index.php?lbservice)

WSDL地址：

[http://www.cobub.com/lbs/index.php?lbservice/wsdl](http://www.cobub.com/lbs/index.php?lbservice%2Fwsdl)

 

下面对Android的版本进行了详细的分析，并介绍了如何使用Cobub的LBS服务。

 

第一步,先去申请一个API key ，[http://www.cobub.com/lbs/?register](http://www.cobub.com/lbs/?register)

          但是前提是要先注册一个Cobub账号，需要Email验证。

第二步，上面申请的API key会在WebService中使用。作为访问的凭证。

             API Key将会用在com.wbtech.cobub.model package下，Constants.java中使用。

 

```
package com.wbtech.cobub.model;

public class Constants {
	public static String serviceURLPrefix = "http://www.cobub.com/lbs/index.php?lbservice/";
	public static String APIKey = "0097da75bc8864672f8dedca5c5c590bdb0d2653";
}
```

 

Key更改完毕就可以直接Run了。

 

下面介绍一下代码： 

Activity自然不用说了，几个界面都在其中。

其中WebService访问部分使用了开源库KSoap2

 

下面对各个代码进行详细的分析：

 

根据基站信息查询地址

 

基站信息主要包含了MNC，MCC，LAC和CELL ID。其中基站的详细信息可以从如下代码中获取：

 

```
private void RefreshCellInfo() {
		try {
			TelephonyManager tm = (TelephonyManager)
			this.getSystemService(Context.TELEPHONY_SERVICE);//
			
			GsmCellLocation location = (GsmCellLocation)tm.getCellLocation();
			LAC = String.valueOf(location.getLac());
			CELLID = String.valueOf(location.getCid());
			
			String MCCMNC = tm.getNetworkOperator();
			
			MNC = MCCMNC.substring(0,3);
			MCC = Integer.valueOf(MCCMNC.substring(3)).toString();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
```

 

 

![](http://dl.iteye.com/upload/attachment/290253/493441fd-f851-3632-a4f0-625bc295b53a.png)

 

![查询的位置结果可以再地图上显示](http://dl.iteye.com/upload/attachment/290259/36b2ac3d-40f0-3ba9-8328-a2dba4a72ef4.png)

 

根据IP地址进行查询

 

直接输入IP地址就可以了。

![根据IP地址进行查询](http://dl.iteye.com/upload/attachment/290261/d4e3e517-a97c-36ae-99ad-62e84c3d2011.png)

 

地图显示

 

![地图显示](http://dl.iteye.com/upload/attachment/290263/25cbff02-7913-3dcb-8d97-31e964b373d9.png)

 

根据地址查询GPS

 

在GPSActivity.java中包含了如何使用GPS，详情可以看看代码：

下面简单介绍一下如何使用：

 

在OnCreate函数中调用了方法initLocation();

 

 

```
private void initLocation() {
			this.mLocationListener = new SampleLocationListener();
		}
```

 

 

SampleLocationListener 是具体实现了Location Changed 的监听。

 

```
 private class SampleLocationListener implements LocationListener {
			public void onLocationChanged(final Location loc) {
				if (loc != null) {
					// if(OpenStreetMapActivity.this.mDoGPSRecordingAndContributing)
					// OpenStreetMapActivity.this.mRouteRecorder.add(loc,
					// OpenStreetMapActivity.this.mNumSatellites);

					GPSActivity.this.onLocationChanged(loc);
				} else {
					GPSActivity.this.onLocationLost();
				}
			}

			public void onStatusChanged(String a, int status, Bundle b) {
				GPSActivity.this.mNumSatellites = b.getInt("satellites",
						0); // LATER Check on an actual device
				// Log.e(DEBUGTAG, "onStatusChanged status = " + status +
				// " satellites = " + b.getInt("satellites", NOT_SET));
				StatusChanged(a, status, b);
			}

			public void onProviderEnabled(String a) {
				Log.e("NETWORK", "onProviderEnabled");
				getBestProvider();
			}

			public void onProviderDisabled(String a) {
				Log.e("NETWORK", "onProviderDisabled");
				getBestProvider();
			}
		}
```

 

下面几个Event就可以实时跟新GPS了

```
public void onLocationLost() {
		}

		public void onLocationChanged(Location loc) {
			
		 latitudeString = String.valueOf(loc.getLatitude());
		 longitudeString = String.valueOf(loc.getLongitude());
		
		 TextView cellText = (TextView)findViewById(R.id.textCellInfo);
		 cellText.setText("经度:"+longitudeString + "\n纬度:"+latitudeString);
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// int cnt = extras.getInt("satellites", Integer.MIN_VALUE);
			// mStatusListener = provider+ " " + status + " " + (cnt >= 0 ? cnt :
			// 0);
			// setTitle();
		}
```

 

另外在OnDestroy方法中别忘记释放就可以了。

```
protected void onDestroy() {
			super.onDestroy();
			getLocationManager().removeUpdates(mLocationListener);
			if (mNetListener != null)
				getLocationManager().removeUpdates(mNetListener);
		}
```

 

 ![](http://dl.iteye.com/upload/attachment/290294/fbd8d75e-3ca2-3960-a376-f66fe666330f.png)

![](http://dl.iteye.com/upload/attachment/290291/8680c779-fecd-3ed4-80f8-8a2d980b3d2d.png)

 

根据地址查询GPS

 

![](http://dl.iteye.com/upload/attachment/290273/073fdda4-6b68-3afb-ad42-6ab5f7c7a121.png)

 

![](http://dl.iteye.com/upload/attachment/290271/676e51e0-0cf4-318f-8a66-ac0675c2d8ef.png)

 

根据WIFI获取位置信息

 

具体实现在WIFIActivity.java中。

以下方法只是获取了所有wifi中信号强度最大的一个。

```
private void RefreshWifiStatus() {
		try {
			WifiManager  wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
			
			if(wifiManager == null)
			{
				Toast.makeText(getApplicationContext(), "WIFI未启用", Toast.LENGTH_SHORT).show();
				return;
			}
			
			List<ScanResult> sResults = wifiManager.getScanResults();
			int maxLevel = -10000;
			if(sResults!=null && sResults.size()>0)
			{
				for(int i=0;i<sResults.size();i++)
				{
					ScanResult sr = sResults.get(i);
					if(sr.level>maxLevel)
					{
						maxLevel = sr.level;
						wifiMacString = sr.BSSID;
						wifiSignalString = String.valueOf(sr.level);
						ssid = sr.SSID;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
```

 

 

![](http://dl.iteye.com/upload/attachment/290283/33093876-74df-332c-b216-60c47d6c7b93.png)

 

 

总结一下，Cobub Service实现了全面的位置查询API，以webservice的形式提供给用户免费使用，是用户在开发中更方便的集成LBS应用。