Android GetLocation 位置定位

定位在真机上模拟的效果会好很多,而且一定要在屋外实验,在室内GPS连不上的,而且onLocationChanged是不会改变的。

首先看看xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GetLocationActivity" >

    <TextView
        android:id="@+id/TV_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
在看java文件

package com.example.getlocation;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class GetLocationActivity extends Activity {

	private TextView tView ;
	private LocationManager locationManager ;
	private String provider ;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_location);
        
        tView = (TextView) findViewById(R.id.TV_ID) ;
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE) ;
        //获取所有的可用位置提供器
        List<String> providerList = locationManager.getProviders(true) ;
        if(providerList.contains(LocationManager.GPS_PROVIDER)){
        	provider = LocationManager.GPS_PROVIDER ;
        	Toast.makeText(this, "GPS", Toast.LENGTH_LONG).show() ;
        }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
        	provider = LocationManager.NETWORK_PROVIDER ;
        	Toast.makeText(this, "N", Toast.LENGTH_LONG).show() ;
        	//Toast.makeText(this, "No Location provider to use", Toast.LENGTH_LONG).show() ;
        }else{
        	//没有位置提供器
        	Toast.makeText(this, "No Location provider to use", Toast.LENGTH_LONG).show() ;
        	return ;
        }
        Location location = locationManager.getLastKnownLocation(provider) ;
        
       
        if(location != null){
        	showLocation(location) ;
        	//Toast.makeText(this, "you dizhi", Toast.LENGTH_LONG).show() ;
        }else{
        	Toast.makeText(this, " meiyou dizhi", Toast.LENGTH_LONG).show() ;
        }
        locationManager.requestLocationUpdates(provider, 1000, 1, locationListener) ;
    }

    
    protected void OnDestroy() {
		super.onDestroy() ;
		if(locationManager != null){
			locationManager.removeUpdates(locationListener) ;
		}
	}
    
    LocationListener locationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			showLocation(arg0) ;
			
		
		}
	};
	
	private void showLocation(Location location){
		String currentPosition = "latitude is" + location.getLatitude() + "\n"
				+ "longitude is" + location.getLongitude() ;
		tView.setText(currentPosition) ;
		Toast.makeText(this, "you dizhill ", Toast.LENGTH_LONG).show() ;
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.get_location, menu);
        return true;
    }
    
}

还要在配置文件添加上去
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission> 
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值