1 gprs定位
package com.android.antking.gps;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class GPSActivity extends Activity {
//声明位置管理对象
private LocationManager locationManager;
//声明位置监听对象
private LocationListener locationListener;
//声明字符串变量
String locationprovider;
//声明显示文本视图组建
private TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得文本视图
textview = (TextView)this.findViewById(R.id.textView1);
try{
//新建Criteria类
Criteria locationcriteria = new Criteria();
//设置精确精度
locationcriteria.setAccuracy(Criteria.ACCURACY_FINE);
//不提供海拔高度信息
locationcriteria.setAltitudeRequired(false);
//不提供方向信息
locationcriteria.setBearingRequired(false);
//允许运营商计费
locationcriteria.setCostAllowed(true);
//设置电池消耗为低耗费
locationcriteria.setPowerRequirement(Criteria.POWER_LOW);
//使用getSystemService()方法获得位置管理器对象
locationManager
=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locationManager.setTestProviderEnabled("gps", true);
Toast.makeText(GPSActivity.this, "getSystemService", Toast.LENGTH_SHORT).show();
//检查gps功能开启
if(checkgps()){
locationprovider
=locationManager.getBestProvider(locationcriteria, true);
Log.d("provider", locationprovider);
//注册位置监听器
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(locationprovider, 1000, 0,locationListener);
}
}catch(Exception e){
Toast.makeText(GPSActivity.this, "异常错误"+e.toString(),Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener{
/**
* 若位置发生变化,onLocationChanged方法被调用
*/
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.i("位置发生变化", "Invoke");
if(location != null){
//获得经度
String latitude = Double.toString(location.getLatitude());//经度
//获得纬度
String longitude = Double.toString(location.getLongitude());//纬度
//在文本框中显示
textview = (TextView)GPSActivity.this.findViewById(R.id.textView1);
textview.setText("经度:"+longitude+"纬度"+latitude);
}
//locationManager.removeUpdates(this);
//locationManager.setTestProviderEnabled("gps", true);
}
//若屏蔽提供商,该方法被调用
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.i("屏蔽提供商", "Invode");
}
//若激活提供商,该方法被调用
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.i("激活提供商", "Invode");
}
//若状态发生变化,该方法被调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.i("状态发生变化", "Invode");
}
}
private boolean checkgps(){
boolean providerEnabled
= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//若被激活,则返回真值
if(providerEnabled ==true){
Toast.makeText(this, "Gps模块活动正常", Toast.LENGTH_SHORT).show();
return true;
}
else{
Toast.makeText(this, "请开启GPS", Toast.LENGTH_SHORT);
return false;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.android.antking.gps"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GPSActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2,谷歌基站定位(获取的是json字符串 有经纬度和城市信息)
package lab.sodino.location;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Google定位的实现.<br/>
* Geolocation的详细信息请参见:<br/>
* <a
* href="
http://code.google.com/apis/gears/geolocation_network_protocol.html">
* <A href="http://code.google.com/apis/gears/geolocation_network_protocol.htmlhttp://code.google.com/apis/gears/geolocation_network_protocol.html</a>
*/
public class LocationAct extends Activity {
private TextView txtInfo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnStart);
txtInfo = (TextView) findViewById(R.id.txtInfo);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
getLocation();
}
});
}
private void getLocation() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();
int cid = gsmCell.getCid();
int lac = gsmCell.getLac();
String netOperator = tm.getNetworkOperator();
int mcc = Integer.valueOf(netOperator.substring(0, 3));
int mnc = Integer.valueOf(netOperator.substring(3, 5));
JSONObject holder = new JSONObject();
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
try {
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("address_language", "zh_CN");
holder.put("request_address", true);
holder.put("radio_type", "gsm");
holder.put("carrier", "HTC");
data.put("cell_id", cid);
data.put("location_area_code", lac);
data.put("mobile_countyr_code", mcc);
data.put("mobile_network_code", mnc);
array.put(data);
holder.put("cell_towers", array);
} catch (JSONException e) {
e.printStackTrace();
}
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("
http://www.google.com/loc/json");
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(holder.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(stringEntity);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = null;
try {
is = httpEntity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
StringBuffer stringBuffer = new StringBuffer();
try {
String result = "";
while ((result = reader.readLine()) != null) {
stringBuffer.append(result);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("[sodino]" + stringBuffer.toString());
txtInfo.setText(stringBuffer.toString());
}
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>