android手機定位在中國還是有些坑的。
首先android的定位方式分為三種:GPS,NET_WORK,PASSIVE
具體問題代碼見:
1.擷取定位管理服務:
LocationManager mgLocation=(LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
2.擷取定位
//GPS定位
Location location_1=mgLocation.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//網絡基站定位
Location location_2=mgLocation.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//手機服務商定位
Location location_3=mgLocation.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
其中坑的是GPS定位,在中國,你在室内開發時,你的手機根本就沒法擷取位置資訊
如果想通過精确的GPS擷取定位需要結合百度SDK,百度地圖,高德地圖等協同顯示
擷取權限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
//設定實時定位間隔
mgLocation.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1000, 0, MainActivity.this);
//其中MainActivity.this表示的是LocationListener,
因為:
MainActivity extends Activity implements LocationListener
//複寫方法:
@Override
public void onLocationChanged(Location location)
location為定位成功後的位置
打開手機GPS的Intent:
Intent intent=new Intent();
intent.setAction(Setting.Action_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
最後介紹一下通過WIFI定位:
首先擷取wifi的mac位址
然後将mac位址發送給google,擷取google分析過來的地理位置
public
classWiFiInfoManager
implements
Serializable {
private
static
final
long
serialVersionUID= -4582739827003032383L;
private
Context context;
public
WiFiInfoManager(Context context) {
super
();
this
.context = context;
}
public
WifiInfo getWifiInfo() {
WifiManager manager = (WifiManager)context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info =
new
WifiInfo();
info.mac =manager.getConnectionInfo().getBSSID();
Log.i( TAG , WIFI MACis: + info.mac);
return
info;
}
public
class
WifiInfo {
public
String mac;
public
WifiInfo() {
super
();
}
}
}
public
staticLocation getWIFILocation(WifiInfo wifi) {
if
(wifi ==
null
) {
Log.i( TAG , wifiis
null
. );
return
null
;
}
DefaultHttpClient client = newDefaultHttpClient();
HttpPost post =
new
HttpPost( http:
//www.google.com/loc/json );
JSONObject holder =
new
JSONObject();
try
{
holder.put( version ,
1.1
.
);
holder.put( host , maps.google.com );
JSONObject data;
JSONArray array =
new
JSONArray();
if
(wifi.mac !=
null
wifi.mac.trim().length()
) {
data =
new
JSONObject();
data.put( mac_address , wifi.mac);
data.put( signal_strength ,
8
);
data.put( age ,
);
array.put(data);
}
holder.put( wifi_towers ,array);
Log.i( TAG , request json: + holder.toString());
StringEntity se = newStringEntity(holder.toString());
post.setEntity(se);
HttpResponse resp =client.execute(post);
int
state =resp.getStatusLine().getStatusCode();
if
(state == HttpStatus.SC_OK) {
HttpEntity entity =resp.getEntity();
if
(entity !=
null
) {
BufferedReader br = newBufferedReader(
newInputStreamReader(entity.getContent()));
StringBuffer sb = newStringBuffer();
String resute = ;
while
((resute =br.readLine()) !=
null
) {
sb.append(resute);
}
br.close();
Log.i( TAG , response json: + sb.toString());
data = newJSONObject(sb.toString());
data = (JSONObject)data.get( location );
Location loc = newLocation(
android.location.LocationManager.NETWORK_PROVIDER);
loc.setLatitude((Double)data.get( latitude ));
loc.setLongitude((Double)data.get( longitude ));
loc.setAccuracy(Float.parseFloat(data.get( accuracy )
.toString()));
loc.setTime(System.currentTimeMillis());
return
loc;
}
else
{
return
null
;
}
}
else
{
Log.v( TAG , state + );
return
null
;
}
}
catch
(Exception e) {
Log.e( TAG ,e.getMessage());
return
null
;
}
}
上面這個方法僅作為學習使用,因為偉大的牆的原因,google 的網站我們是連不上去滴
注:
LocationManager.PASSIVE_PROVIDER定位貌似隻能使用一次,不能連續監聽,下次使用還需要重新調用方法
具體并未嘗試太多,有新發現可以共同讨論