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

 
package com.studio.android.chp08.ex01;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CurrentLocation extends Activity {
    /** Called when the activity is first created. */
	
	
	  private Location local=null;
	  private int i=0;
	  
	 /**
     * LocationListener 是一个接口
     * new LocationListener() 去实例化这个接口
     * new LocationListener() {}对接口的方法进行实现
     * 
     * 整体而言是个 成员变量 复制语句,可以放到 前面去
     * 这个成员变量 是 LocationListener 类型
     */
	   private final LocationListener locationListener = new LocationListener() {
	    	public void onLocationChanged(Location location) {
	    		updateWithNewLocation(location);//地址改变
	    		System.out.println("the onLocationChanged");
	    	}
	    	public void onProviderDisabled(String provider){
	    		updateWithNewLocation(null);//地址失效
	    		System.out.println("the onProviderDisabled");
	    	}
	    	public void onProviderEnabled(String provider){
	    		System.out.println("the onProviderEnabled");
	    		
	    	}
	    	
	    	public void onStatusChanged(String provider, int status,Bundle extras){ 
	    		System.out.println("the onStatusChanged");
	    		
	    		
	    	}
	    };

	    
	    public Location getLocal() {
			return local;
		}
	
	
		public void setLocal(Location local) {
			this.local = local;
		}
	
	
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        
	        setContentView(R.layout.main);  //选择布局,用 main 里面的布局方式
	        
	        Button btv=(Button)findViewById(R.id.GoSecond);
	        
	        LocationManager locationManager;
	        String serviceName = Context.LOCATION_SERVICE;
	        locationManager = (LocationManager)getSystemService(serviceName);
	        String provider = LocationManager.GPS_PROVIDER;
	        
	     
	        
	        Location location = locationManager.getLastKnownLocation(provider);
	        
	        if(location!=null){
	        		this.setLocal(location);
	        	   this.updateWithNewLocation(location);
	   	        
	        	
	        }
	     
	    
	        locationManager.requestLocationUpdates(provider, 2000, 0, 
	        		locationListener);//注册了监听器
	        
	        
	        btv.setOnClickListener(new View.OnClickListener(){
	
				public void onClick(View v) {
					// TODO 实现这个接口的方法
					
					Intent intent = new Intent();
					
					if(CurrentLocation.this.local!=null){
					//这个是海拔 啊 老兄	double alti=CurrentLocation.this.local.getAltitude();
						
						double lati = CurrentLocation.this.local.getLatitude();
						
						double logti=CurrentLocation.this.local.getLongitude();
						
						intent.putExtra("Lati",lati);
						intent.putExtra("Lnti", logti);
					}
	                intent.setClass(CurrentLocation.this, LocationName.class);   
	                startActivity(intent);   
	                      
	
				}   
	  
	               
	        });   
	  
	        
	        
	    }
	   
	    
	    private void updateWithNewLocation(Location location) {
	    	
	    	String latLongString;
	    	TextView myLocationText;
	    	
	    	myLocationText = (TextView)findViewById(R.id.myLocationText);
	    	if (location != null) {
		    	double lat = location.getLatitude();
		    	double lng = location.getLongitude();
		    	
		    	this.setLocal(location);//更新 本类的location
		    	System.out.println(" the lat is "+lat+"  the lng is "+lng);
		    	
		    	latLongString = "第  "+ ++i+" 次  更新    纬度是:" + lat + "\n经度:" + lng;
		    	
	    	} else {
	    		latLongString = "无法获取地理信息";
	    	}
	    	
	    	myLocationText.setText("您当前的位置是:\n" +latLongString);
	    }
}


 

 

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

 

 manifest

package com.studio.android.chp08.ex01;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;

/**
 * 
 * 得到具体的 地名
 * @author Administrator
 *
 */

public class LocationName extends Activity {
	private   String  latLongString=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addressname);
		
		List<Address> addList = null;
        Geocoder ge = new Geocoder(this,Locale.CHINA);
      
        TextView  myLocationText=(TextView) findViewById(R.id.CityName);
        Intent GetIntent=this.getIntent();
        
        
        if(GetIntent==null){
        	
        	System.out.println(" it is  null");
        }
        
        else if (GetIntent!=null){
        	System.out.println("it is not null");
        	
        	double lati2=GetIntent.getDoubleExtra("Lati", 119.3090049);
        	double longti2=GetIntent.getDoubleExtra("Lnti", 26.0972567);
        
        	
            
            try {
                addList = ge.getFromLocation(lati2,longti2, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    
        if(addList!=null && addList.size()>0){
        	latLongString=new String();
            for(int i=0; i<addList.size(); i++){
                Address ad = addList.get(i);
                latLongString += "\n";
               latLongString +=ad.getAddressLine(0)+ad.getAddressLine(1)+ad.getAddressLine(2);
                
     
            }
        
        }
        
        myLocationText.setText("您当前的位置是:\n" +latLongString);


	}
	
	
	

}


xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.studio.android.chp08.ex01"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CurrentLocation"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <activity android:name=".LocationName"
                  android:label="@string/app_name">
      
        </activity>
    </application>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!--  uses-permission 用来权限说明的  -->
</manifest> 

 

 


address.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    
    <TextView
    
     android:id="@+id/CityName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
    
    />
    <ListView 
    
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

 

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  
    <!--  开始放组建  -->
<TextView  
	android:id="@+id/myLocationText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
      android:text="@string/hello"
    />
    
    <Button 
    android:id="@+id/getLocation"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/GetLocation"
    
     />
     
     <Button 
     android:id="@+id/GoSecond"
     
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     
     android:text="@string/GoSecond"
     />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值