用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

定位

2013-05-07 作者: 维尼举报

[android]代码库

package com.hj.gps;

import java.io.IOException;

import java.util.List;
import java.util.Locale;

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

public class GPS extends Activity {
	public Activity self = GPS.this;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps);
        LocationManager locationManager;
		
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager)getSystemService(context);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(false);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		String provider = locationManager.getBestProvider(criteria, true);
		
		Location location = locationManager.getLastKnownLocation(provider);
		
		updateWithNewLocation(location);
		
		locationManager.requestLocationUpdates(provider, 3000, 0, locationistener);
		
	}
		
private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id.myLocation);

String addressString = "没有找到地址\n";

if(location!=null){
	double lat  = location.getLatitude();
	double lng = location.getLongitude();
	
	latLongString = "纬度:"+lat+"\n经度:"+lng;
	
	double latitude  = location.getLatitude();
	double longitude = location.getLongitude();
	
	Geocoder gc = new Geocoder(self,Locale.getDefault());
	
	try {
		List<Address> addresses = gc.getFromLocation(latitude,longitude,1);
		StringBuilder sb = new StringBuilder();
		
		if(addresses.size()>0){
			Address address = addresses.get(0);
			for(int i=0;i<address.getMaxAddressLineIndex();i++){
				sb.append(address.getAddressLine(i)).append("\n");
			}
//			sb.append(address.getLocality()).append("\n");
//			sb.append(address.getLocality()).append("\n");
//			sb.append(address.getCountryName());
			addressString = sb.toString();
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}else{
	latLongString = "没有找到坐标。\n";
}

myLocationText.setText("您当前的坐标如下:\n"+latLongString+"\n"+addressString);
}

private LocationListener locationistener = new LocationListener(){

//当坐标改变时触发此函数
public void onLocationChanged(Location location){
	updateWithNewLocation(location);
}
//Provider禁用是触发此函数          比如GPS被关闭
public void onProviderDisabled(String provider) {
	// TODO Auto-generated method stub
	
}

//Provider启用是触发此函数,比如Gps被打开
public void onProviderEnabled(String provider) {
	// TODO Auto-generated method stub
	
}
//Provider的状态在可用、暂时不可用、和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider, int status, Bundle extras) {
	// TODO Auto-generated method stub
	
}
};


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gps, menu);
        return true;
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...