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 ; |
} |
} |
中级程序员
by: 坏人 发表于:2013-05-15 13:55:23 顶(0) | 踩(0) 回复
怎么没截图的呢
回复评论