准备完成的GPS相机的部分,GPS部分…

准备完成的GPS相机的部分,GPS部分已经完成了小部分

这个,。。。小部分指的是得到当前GPS的坐标,这个没问题,得到的是小数点后8位精确度的latitude和longtitude。

但是另一个功能至上次仔细研究还没有完成。。。。就是根据当前GPS坐标查询地理信息,使用的方法是在线的网络查询,代码方面可能有待仔细研究,但我找到了相应的代码。

下面贴出完成的部分:
================================== =========================== ===========================
首先是layout下面的XML:activity_main_activity_gpstest.xml
================================== =========================== ===========================
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     tools:context=".MainActivity_gpstest" >

  6.     <Button
  7.         android:id="@+id/button1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_alignParentLeft="true"
  11.         android:layout_alignParentTop="true"
  12.         android:layout_marginLeft="19dp"
  13.         android:layout_marginTop="22dp"
  14.         android:text="获得GPS" />

  15.     <TextView
  16.         android:id="@+id/textView2"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_alignBaseline="@+id/button1"
  20.         android:layout_alignBottom="@+id/button1"
  21.         android:layout_marginLeft="46dp"
  22.         android:layout_toRightOf="@+id/button1"
  23.         android:text="当前GPS信息" />

  24.     <Button
  25.         android:id="@+id/button2"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:layout_alignLeft="@+id/button1"
  29.         android:layout_below="@+id/button1"
  30.         android:layout_marginTop="32dp"
  31.         android:layout_toLeftOf="@+id/textView2"
  32.         android:text="查地理信息" />

  33.     <EditText
  34.         android:id="@+id/editText1"
  35.         android:layout_width="wrap_content"
  36.         android:layout_height="wrap_content"
  37.         android:layout_below="@+id/button2"
  38.         android:layout_marginTop="51dp"
  39.         android:layout_toLeftOf="@+id/button3"
  40.         android:ems="10"
  41.         android:hint="请输入中文位置" >

  42.         <requestFocus />
  43.     </EditText>

  44.     <Button
  45.         android:id="@+id/button3"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:layout_alignParentRight="true"
  49.         android:layout_alignTop="@+id/editText1"
  50.         android:text="根据输入查经纬" />

  51.     <TextView
  52.         android:id="@+id/textView1"
  53.         android:layout_width="wrap_content"
  54.         android:layout_height="wrap_content"
  55.         android:layout_alignBottom="@+id/button2"
  56.         android:layout_alignLeft="@+id/textView2"
  57.         android:layout_marginBottom="14dp"
  58.         android:text="当前位置:" />

  59.     <TextView
  60.         android:id="@+id/textView4"
  61.         android:layout_width="wrap_content"
  62.         android:layout_height="wrap_content"
  63.         android:layout_alignParentBottom="true"
  64.         android:layout_alignParentRight="true"
  65.         android:text="by prince" />

  66.     <TextView
  67.         android:id="@+id/textView3"
  68.         android:layout_width="wrap_content"
  69.         android:layout_height="wrap_content"
  70.         android:layout_alignRight="@+id/button2"
  71.         android:layout_below="@+id/editText1"
  72.         android:layout_marginTop="22dp"
  73.         android:text="查询经纬度:" />

  74.     <EditText
  75.         android:id="@+id/editText2"
  76.         android:layout_width="wrap_content"
  77.         android:layout_height="wrap_content"
  78.         android:layout_alignParentLeft="true"
  79.         android:layout_alignRight="@+id/editText1"
  80.         android:layout_below="@+id/textView3"
  81.         android:layout_marginTop="29dp"
  82.         android:ems="10"
  83.         android:hint="请输入经纬" />

  84.     <Button
  85.         android:id="@+id/button4"
  86.         android:layout_width="wrap_content"
  87.         android:layout_height="wrap_content"
  88.         android:layout_alignBaseline="@+id/editText2"
  89.         android:layout_alignBottom="@+id/editText2"
  90.         android:layout_alignLeft="@+id/button3"
  91.         android:layout_alignParentRight="true"
  92.         android:text="根据经纬查地理" />

  93.     <TextView
  94.         android:id="@+id/textView5"
  95.         android:layout_width="wrap_content"
  96.         android:layout_height="wrap_content"
  97.         android:layout_alignLeft="@+id/textView3"
  98.         android:layout_below="@+id/button4"
  99.         android:layout_marginTop="17dp"
  100.         android:text="查询地理:" />

  101. </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> mData=new ArrayList>();
  •         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);
  •                    
  •             });
  •     }
  •      
  •  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值