实现的第一种 用GPS 定位的 代码


  1. package com.studio.Android.chp08.ex01;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.location.Criteria;  
  8. import android.location.Location;  
  9. import android.location.LocationListener;  
  10. import android.location.LocationManager;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15.   
  16. public class CurrentLocation extends Activity {  
  17.     /** Called when the activity is first created. */  
  18.       
  19.       
  20.       private Location local=null;  
  21.       private int i=0;  
  22.         
  23.      /** 
  24.      * LocationListener 是一个接口 
  25.      * new LocationListener() 去实例化这个接口 
  26.      * new LocationListener() {}对接口的方法进行实现 
  27.      *  
  28.      * 整体而言是个 成员变量 复制语句,可以放到 前面去 
  29.      * 这个成员变量 是 LocationListener 类型 
  30.      */  
  31.        private final LocationListener locationListener = new LocationListener() {  
  32.             public void onLocationChanged(Location location) {  
  33.                 updateWithNewLocation(location);//地址改变   
  34.                 System.out.println("the onLocationChanged");  
  35.             }  
  36.             public void onProviderDisabled(String provider){  
  37.                 updateWithNewLocation(null);//地址失效   
  38.                 System.out.println("the onProviderDisabled");  
  39.             }  
  40.             public void onProviderEnabled(String provider){  
  41.                 System.out.println("the onProviderEnabled");  
  42.                   
  43.             }  
  44.               
  45.             public void onStatusChanged(String provider, int status,Bundle extras){   
  46.                 System.out.println("the onStatusChanged");  
  47.                   
  48.                   
  49.             }  
  50.         };  
  51.   
  52.           
  53.         public Location getLocal() {  
  54.             return local;  
  55.         }  
  56.       
  57.       
  58.         public void setLocal(Location local) {  
  59.             this.local = local;  
  60.         }  
  61.       
  62.       
  63.         @Override  
  64.         public void onCreate(Bundle savedInstanceState) {  
  65.             super.onCreate(savedInstanceState);  
  66.               
  67.               
  68.             setContentView(R.layout.main);  //选择布局,用 main 里面的布局方式   
  69.               
  70.             Button btv=(Button)findViewById(R.id.GoSecond);  
  71.               
  72.             LocationManager locationManager;  
  73.             String serviceName = Context.LOCATION_SERVICE;  
  74.             locationManager = (LocationManager)getSystemService(serviceName);  
  75.             String provider = LocationManager.GPS_PROVIDER;  
  76.               
  77.            
  78.               
  79.             Location location = locationManager.getLastKnownLocation(provider);  
  80.               
  81.             if(location!=null){  
  82.                     this.setLocal(location);  
  83.                    this.updateWithNewLocation(location);  
  84.                   
  85.                   
  86.             }  
  87.            
  88.           
  89.             locationManager.requestLocationUpdates(provider, 20000,   
  90.                     locationListener);//注册了监听器   
  91.               
  92.               
  93.             btv.setOnClickListener(new View.OnClickListener(){  
  94.       
  95.                 public void onClick(View v) {  
  96.                     // TODO 实现这个接口的方法www.bkjia.com  
  97.                       
  98.                     Intent intent = new Intent();  
  99.                       
  100.                     if(CurrentLocation.this.local!=null){  
  101.                     //这个是海拔 啊 老兄    double alti=CurrentLocation.this.local.getAltitude();   
  102.                           
  103.                         double lati = CurrentLocation.this.local.getLatitude();  
  104.                           
  105.                         double logti=CurrentLocation.this.local.getLongitude();  
  106.                           
  107.                         intent.putExtra("Lati",lati);  
  108.                         intent.putExtra("Lnti", logti);  
  109.                     }  
  110.                     intent.setClass(CurrentLocation.this, LocationName.class);     
  111.                     startActivity(intent);     
  112.                             
  113.       
  114.                 }     
  115.         
  116.                      
  117.             });     
  118.         
  119.               
  120.               
  121.         }  
  122.          
  123.           
  124.         private void updateWithNewLocation(Location location) {  
  125.               
  126.             String latLongString;  
  127.             TextView myLocationText;  
  128.               
  129.             myLocationText = (TextView)findViewById(R.id.myLocationText);  
  130.             if (location != null) {  
  131.                 double lat = location.getLatitude();  
  132.                 double lng = location.getLongitude();  
  133.                   
  134.                 this.setLocal(location);//更新 本类的location   
  135.                 System.out.println(" the lat is "+lat+"  the lng is "+lng);  
  136.                   
  137.                 latLongString = "第  "+ ++i+" 次  更新    纬度是:" + lat + "\n经度:" + lng;  
  138.                   
  139.             } else {  
  140.                 latLongString = "无法获取地理信息";  
  141.             }  
  142.               
  143.             myLocationText.setText("您当前的位置是:\n" +latLongString);  
  144.         }  
  145. }   

第二个界面 显示 地址解析的界面

manifest

  1. package com.studio.android.chp08.ex01;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5. import java.util.Locale;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.location.Address;  
  10. import android.location.Geocoder;  
  11. import android.os.Bundle;  
  12. import android.widget.TextView;  
  13.   
  14. /** 
  15.  *  
  16.  * 得到具体的 地名www.bkjia.com 
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20.   
  21. public class LocationName extends Activity {  
  22.     private   String  latLongString=null;  
  23.       
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         // TODO Auto-generated method stub   
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.addressname);  
  29.           
  30.         List<Address> addList = null;  
  31.         Geocoder ge = new Geocoder(this,Locale.CHINA);  
  32.         
  33.         TextView  myLocationText=(TextView) findViewById(R.id.CityName);  
  34.         Intent GetIntent=this.getIntent();  
  35.           
  36.           
  37.         if(GetIntent==null){  
  38.               
  39.             System.out.println(" it is  null");  
  40.         }  
  41.           
  42.         else if (GetIntent!=null){  
  43.             System.out.println("it is not null");  
  44.               
  45.             double lati2=GetIntent.getDoubleExtra("Lati"119.3090049);  
  46.             double longti2=GetIntent.getDoubleExtra("Lnti"26.0972567);  
  47.           
  48.               
  49.               
  50.             try {  
  51.                 addList = ge.getFromLocation(lati2,longti2, 1);  
  52.             } catch (IOException e) {  
  53.                 // TODO Auto-generated catch block   
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.           
  58.       
  59.         if(addList!=null && addList.size()>0){  
  60.             latLongString=new String();  
  61.             for(int i=0; i<addList.size(); i++){  
  62.                 Address ad = addList.get(i);  
  63.                 latLongString += "\n";  
  64.                latLongString +=ad.getAddressLine(0)+ad.getAddressLine(1)+ad.getAddressLine(2);  
  65.                   
  66.        
  67.             }  
  68.           
  69.         }  
  70.           
  71.         myLocationText.setText("您当前的位置是:\n" +latLongString);  
  72.   
  73.   
  74.     }  
  75.       
  76.       
  77.       
  78.   

xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.studio.android.chp08.ex01"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".CurrentLocation"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.        <activity android:name=".LocationName"  
  15.                   android:label="@string/app_name">  
  16.         
  17.         </activity>  
  18.     </application>  
  19.   
  20.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
  21.     <!--  uses-permission 用来权限说明的  -->  
  22. </manifest> 

address.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.       
  8.     <TextView  
  9.       
  10.      android:id="@+id/CityName"  
  11.      android:layout_width="fill_parent"  
  12.      android:layout_height="fill_parent"  
  13.       
  14.     />  
  15.     <ListView   
  16.       
  17.     android:id="@+id/list"  
  18.     android:layout_width="fill_parent"  
  19.     android:layout_height="fill_parent"  
  20.     />  
  21. </LinearLayout>  

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >    
  7.     <!--  开始放组建  -->  
  8. <TextView    
  9.     android:id="@+id/myLocationText"  
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.       android:text="@string/hello"  
  13.     />  
  14.       
  15.     <Button   
  16.     android:id="@+id/getLocation"  
  17.     android:layout_width="fill_parent"   
  18.     android:layout_height="wrap_content"   
  19.     android:text="@string/GetLocation"  
  20.       
  21.      />  
  22.        
  23.      <Button   
  24.      android:id="@+id/GoSecond"  
  25.        
  26.      android:layout_width="fill_parent"   
  27.      android:layout_height="wrap_content"   
  28.        
  29.      android:text="@string/GoSecond"  
  30.      />  
  31. </LinearLayout>  

相关内容