准备完成的GPS相机的部分,GPS部分已经完成了小部分
这个,。。。小部分指的是得到当前GPS的坐标,这个没问题,得到的是小数点后8位精确度的latitude和longtitude。
但是另一个功能至上次仔细研究还没有完成。。。。就是根据当前GPS坐标查询地理信息,使用的方法是在线的网络查询,代码方面可能有待仔细研究,但我找到了相应的代码。
下面贴出完成的部分:
==================================
===========================
===========================
首先是layout下面的XML:activity_main_activity_gpstest.xml
==================================
===========================
===========================
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools" -
android:layout_width="fill_parent" -
android:layout_height="fill_parent" -
tools:context=".MainActivity_gpstest" > -
<Button -
android:id="@+id/button1" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentLeft="true" -
android:layout_alignParentTop="true" -
android:layout_marginLeft="19dp" -
android:layout_marginTop="22dp" -
android:text="获得GPS" /> -
<TextView -
android:id="@+id/textView2" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignBaseline="@+id/button1" -
android:layout_alignBottom="@+id/button1" -
android:layout_marginLeft="46dp" -
android:layout_toRightOf="@+id/button1" -
android:text="当前GPS信息" /> -
<Button -
android:id="@+id/button2" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignLeft="@+id/button1" -
android:layout_below="@+id/button1" -
android:layout_marginTop="32dp" -
android:layout_toLeftOf="@+id/textView2" -
android:text="查地理信息" /> -
<EditText -
android:id="@+id/editText1" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_below="@+id/button2" -
android:layout_marginTop="51dp" -
android:layout_toLeftOf="@+id/button3" -
android:ems="10" -
android:hint="请输入中文位置" > -
<requestFocus /> -
</EditText> -
<Button -
android:id="@+id/button3" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentRight="true" -
android:layout_alignTop="@+id/editText1" -
android:text="根据输入查经纬" /> -
<TextView -
android:id="@+id/textView1" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignBottom="@+id/button2" -
android:layout_alignLeft="@+id/textView2" -
android:layout_marginBottom="14dp" -
android:text="当前位置:" /> -
<TextView -
android:id="@+id/textView4" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentBottom="true" -
android:layout_alignParentRight="true" -
android:text="by prince" /> -
<TextView -
android:id="@+id/textView3" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignRight="@+id/button2" -
android:layout_below="@+id/editText1" -
android:layout_marginTop="22dp" -
android:text="查询经纬度:" /> -
<EditText -
android:id="@+id/editText2" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentLeft="true" -
android:layout_alignRight="@+id/editText1" -
android:layout_below="@+id/textView3" -
android:layout_marginTop="29dp" -
android:ems="10" -
android:hint="请输入经纬" /> -
<Button -
android:id="@+id/button4" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignBaseline="@+id/editText2" -
android:layout_alignBottom="@+id/editText2" -
android:layout_alignLeft="@+id/button3" -
android:layout_alignParentRight="true" -
android:text="根据经纬查地理" /> -
<TextView -
android:id="@+id/textView5" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignLeft="@+id/textView3" -
android:layout_below="@+id/button4" -
android:layout_marginTop="17dp" -
android:text="查询地理:" /> - </RelativeLayout>
==================================
===========================
===========================
再然后是两个文件,首先是GPS底层的文件:GoogleGeographyQuery.java
这个类中封装了GPS 查询的方法,包括包括lat lng查 地理信息
,地理位置查 lat lng
==================================
===========================
===========================
- package com.example.gpslocationtest;
//gps查询,包括lat lng查 地理信息 ,地理位置查 lat lng - import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.util.Log;
- public class GoogleGeographyQuery{
-
static double lng; -
static double lat; -
-
public static double[] jingweidu(String AddressName){ -
//List -
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + AddressName + "&sensor=false"; -
HttpClient client = new DefaultHttpClient(); -
HttpGet get = new HttpGet(url); -
try { -
HttpResponse response = client.execute(get); -
HttpEntity entity = response.getEntity(); -
InputStream input = entity.getContent(); -
int t; -
StringBuffer buffer = new StringBuffer(); -
while ((t = input.read()) != -1) { -
buffer.append((char) t); -
-
} -
//tv.setText(buffer); -
// json解析 -
JSONObject object = new JSONObject(buffer.toString()); -
JSONObject location = object.getJSONArray("results") -
.getJSONObject(0)// 获得中括号的内容 -
.getJSONObject("geometry")// 获得大括号中的内容 -
.getJSONObject("location"); -
lng = location.getDouble("lng"); -
lat = location.getDouble("lat"); -
Log.i("HHJ", "经纬度是 : "+lng+" "+lat); -
double[] data ={lng,lat}; -
return data; -
} catch (ClientProtocolException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} catch (IOException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} catch (JSONException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} -
return null; -
-
} -
-
public static String geocodeAddr(String latitude, String longitude) { -
String addr = ""; -
// 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址 -
// 密钥可以随便写一个key=abc -
// output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析 -
String url = String.format("http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",latitude, longitude); -
URL myURL = null; -
URLConnection httpsConn = null; -
try { -
myURL = new URL(url); -
} catch (MalformedURLException e) { -
e.printStackTrace(); -
return null; -
} -
try { -
httpsConn = (URLConnection) myURL.openConnection(); -
if (httpsConn != null) { -
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8"); -
BufferedReader br = new BufferedReader(insr); -
String data = null; -
if ((data = br.readLine()) != null) { -
System.out.println(data); -
String[] retList = data.split(","); -
if (retList.length > 2 && ("200".equals(retList[0]))) { -
addr = retList[2]; -
addr = addr.replace("\"", ""); -
} else { -
addr = ""; -
} -
Log.i("HHJ", "123 : "+addr); -
} -
insr.close(); -
} -
} catch (IOException e) { -
e.printStackTrace(); -
return null; -
} -
return addr; -
} - }
==================================
===========================
===========================
最后是主函数,用以操作上面的底层,得到当前的GPS 和地理信息:MainActivity_gpstest.java
当然,实现的只有部分,也就是得到GPS坐标位置,查询地理信息暂时没辙。。。。
==================================
===========================
===========================
- package com.example.gpslocationtest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity_gpstest extends Activity {
-
-
//创建lcoationManager对象 -
private LocationManager manager; -
private static final String TAG = "LOCATION DEMO"; -
public static String latnow=""; -
public static String lngnow=""; -
-
-
@Override -
public void onCreate(final Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.activity_main_activity_gpstest); -
//final TextView textview1=(TextView) findViewById(R.id.textView2);//显示 -
-
Button button1=(Button) findViewById(R.id.button1); //按钮1的活动 -
button1.setOnClickListener(new OnClickListener() { - @Override
- public void onClick(View v) {
- onCreate(savedInstanceState);
- }
- });
-
-
Button button2=(Button) findViewById(R.id.button2); //按钮2的活动 -
button2.setOnClickListener(new OnClickListener() { - @Override
- public void onClick(View v) {
- //
- if(latnow!=""&&lngnow!="")
- {
- String Locationnow=GoogleGeographyQuery.geocodeAddr(latnow, lngnow);
- if( Locationnow!=null)
- {
- //final TextView textviewloca=(TextView) findViewById(R.id.textView1);
- refreshUI2(Locationnow);
- }else{refreshUI2("获取失败,请检查网络");}
- }
- else
- {
- refreshUI2("经纬度获取失败,请检查GPS");
- }
- }
- });
-
-
-
manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //获取系统的位置服务, -
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); -
//第一次获得设备的位置 -
updateLocation(location); -
//重要函数,监听数据测试 -
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10,locationListener); -
-
-
} -
-
public void onPause(){ -
super.onPause(); -
//LocationManager.removeListener(locationListener); //出现错误,待解决,没有相关方法 -
- }
-
-
//创建一个事件监听器 -
private final LocationListener locationListener = new LocationListener() { -
public void onLocationChanged(Location location) { //位置改变触发 -
updateLocation(location); -
-
} -
public void onProviderDisabled(String provider){ //GPS不可使用触发 -
updateLocation(null); -
String str1="GPS不可使用~~"; -
refreshUI(str1); -
Log.i(TAG, "Provider now is disabled.."); -
} -
public void onProviderEnabled(String provider){ //GPS可以使用触发 -
Log.i(TAG, "Provider now is enabled.."); -
String str1="GPS可以使用~~"; -
refreshUI(str1); -
} -
public void onStatusChanged(String provider, int status,Bundle extras){ } -
}; -
- //获取用户位置的函数,利用Log显示
-
private void updateLocation(Location location) { -
String latLng; -
String StringLat=""; -
String StringLng=""; -
-
if (location != null) { -
double lat = location.getLatitude(); //lat纬度 lng经度 -
double lng = location.getLongitude(); -
-
latLng = "Latitude:" + lat + " Longitude:" + lng; -
StringLat=""+lat; -
StringLng=""+lng; -
} -
else { -
latLng = "Can't access your location!~~~"; -
StringLat=""; -
StringLng=""; -
} -
Log.i(TAG, "The location has changed.."); -
Log.i(TAG, "Your Location:" +latLng); -
-
MainActivity_gpstest.latnow=StringLat; -
MainActivity_gpstest.lngnow=StringLng; -
-
refreshUI(latLng);//更新UI -
} -
-
-
-
public void refreshUI(final String str)//更新textView2 -
{ -
MainActivity_gpstest.this.runOnUiThread( //一种简单更新UI的线程,this 指的是当前activity -
new Runnable() { -
@Override -
public void run() { -
// refresh ui 的操作代码 -
final TextView textview1=(TextView) findViewById(R.id.textView2);//显示 -
textview1.setText(str); -
} -
}); -
} -
-
public void refreshUI2(final String str)//更新textView1 -
{ -
MainActivity_gpstest.this.runOnUiThread( //一种简单更新UI的线程,this 指的是当前activity -
new Runnable() { -
@Override -
public void run() { -
// refresh ui 的操作代码 -
final TextView textview1=(TextView) findViewById(R.id.textView1);//显示 -
textview1.setText(str); -
} -
}); -
} -
- }
-