android获取cpu信息 4.4上如何获取经纬度信息

GPS 获得 经纬度 并得到该 坐标 精确地址
- 海风轻轻吹 - ITeye技术网站
博客分类:
package cn.xx.
import java.io.IOE
import java.util.L
import java.util.L
import android.app.A
import android.content.C
import android.location.A
import android.location.C
import android.location.G
import android.location.L
import android.location.LocationL
import android.location.LocationM
import android.os.B
import android.widget.TextV
import android.widget.T
public class CurrentLocation extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationM
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(serviceName);
String provider = LocationManager.GPS_PROVIDER;
String provider = "gps";
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
public void onProviderEnabled(String provider) {
public void onStatusChanged(String provider, int status, Bundle extras) {
private void updateWithNewLocation(Location location) {
String latLongS
TextView myLocationT
myLocationText = (TextView) findViewById(R.id.myLocationText);
Thread.sleep(0);//因为真机获取gps数据需要一定的时间,为了保证获取到,采取系统休眠的延迟方法
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Geocoder geocoder=new Geocoder(this);
Geocoder geocoder = new Geocoder(this, Locale.CHINA);
List places =
Thread.sleep(2000);
places = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
Thread.sleep(2000);
Toast.makeText(CurrentLocation.this, places.size()+"", Toast.LENGTH_LONG).show();
System.out.println(places.size()+"");
} catch (Exception e) {
e.printStackTrace();
String placename = "";
if (places != null && places.size() & 0) {
// placename=((Address)places.get(0)).getLocality();
//一下的信息将会具体到某条街
//其中getAddressLine(0)表示国家,getAddressLine(1)表示精确到某个区,getAddressLine(2)表示精确到具体的街
placename = ((Address) places.get(0)).getAddressLine(0) + ", " + System.getProperty("line.separator")
+ ((Address) places.get(0)).getAddressLine(1) + ", "
+ ((Address) places.get(0)).getAddressLine(2);
latLongString = "纬度:" + lat + "/n经度:" +
Toast.makeText(CurrentLocation.this, placename, Toast.LENGTH_LONG).show();
latLongString = "无法获取地理信息";
myLocationText.setText("您当前的位置是:/n" + latLongString);
又有其他项目,还没着手这个GPS功能,没来得及试
浏览: 186376 次
来自: 杭州
大爱,简直是大爱!!困扰这么久以来的问题,虽然知道是CSS的问 ...
有demo嘛!邮箱:think_world@foxmail.c ...
多谢分享,今天老师刚被布置的任务让实现自动生成java实体类 ...
邮箱十分感谢页面导航:
→ 正文内容 android gps定位示例
android通过gps获取定位的位置数据和gps经纬度
这篇文章主要介绍了android通过gps获取定位的位置数据示例,大家参考使用吧
代码如下:package com.action.android_import android.location.Limport android.location.LocationLimport android.location.LocationMimport android.os.Bimport android.app.Aimport android.view.Mimport android.widget.TextV
public class MainActivity extends Activity {private Location location=@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得位置服务的名称String serviceName = this.LOCATION_SERVICE;//获得位置服务的管理对象LocationManager locationManager = (LocationManager)getSystemService(serviceName);// 通过GPS获取定位的位置数据location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);updateToNewLocation(location);
&&& /**服务管理对象的监听器*/&&& //参数1:定位的方式&& 参数2:监听更新间隔时间(ms)& 参数3:监听更新的距离(m) 参数4:监听的方法&&& locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 2000, 10, new LocationListener() {
&&&&&&& public void onStatusChanged(String provider, int status, Bundle extras) {&&&&&&& }&&&&&&& public void onProviderEnabled(String provider) {&&&&&&& }&&&&&&& public void onProviderDisabled(String provider) {&&&&&&& }&&&&&&& public void onLocationChanged(Location location) {&&&&&&&&&&& updateToNewLocation(location);&&&&&&& }&&& });}private void updateToNewLocation(Location location) {
&&& TextView tv1;&&& tv1 = (TextView) this.findViewById(R.id.tv1);&&& if (location != null) {&&&&&&& double& latitude = location.getLatitude();&&&&&&& double longitude= location.getLongitude();&&&&&&& tv1.setText("经纬度为:n"+"维度:" +& latitude+ "n经度" + longitude);&&& } else {&&&&&&& tv1.setText("无法获取地理信息");&&& }
}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {&&& getMenuInflater().inflate(R.menu.main, menu);&&&}
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行Android中通过当前经纬度获得城市_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android中通过当前经纬度获得城市
来源:Linux社区&
作者:love_xiaozhao
中通过当前经纬度获得城市
package com.&
import java.io.InputStreamR&import java.net.HttpURLC&import java.net.URL;&import javax.xml.parsers.SAXP&import javax.xml.parsers.SAXParserF&import org.xml.sax.A&import org.xml.sax.InputS&import org.xml.sax.SAXE&import org.xml.sax.XMLR&import org.xml.sax.helpers.DefaultH&import android.content.C&import android.location.L&import android.location.LocationM&public class GetCity {& /**&
* 借助Google MAP 通过用户当前经纬度 获得用户当前城市&
*/& static final String GOOGLE_MAPS_API_KEY = "abcdefg";&&private LocationManager locationM& private Location currentL& private String city="全国";& public GetCity(Context context) {&
this.locationManager = (LocationManager) context& &
.getSystemService(Context.LOCATION_SERVICE);&
//只是简单的获取城市 不需要实时更新 所以这里先注释&//& this.locationManager.requestLocationUpdates(&//& & LocationManager.GPS_PROVIDER,& 1000, 0,&//& & new LocationListener() {&//& &
public void onLocationChanged(Location loc) {&//& & & //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 &//& & & // Save the latest location&//& & & currentLocation =&//& & & // Update the latitude & longitude TextViews&//& & & System.out&//& & & & .println("getCity()"&//& & & & & + (loc.getLatitude() + " " + loc&//& & & & & & .getLongitude()));&//& &
}&//&//& &
public void onProviderDisabled(String arg0) {&//& & & System.out.println(".onProviderDisabled(关闭)"+arg0);&//& &
}&//&//& &
public void onProviderEnabled(String arg0) {&//& & & System.out.println(".onProviderEnabled(开启)"+arg0);&//& &
}&//&//& &
public void onStatusChanged(String arg0, int arg1,&//& & &
Bundle arg2) {&//& & & System.out.println(".onStatusChanged(Provider的转态在可用、" +&//& & & & "暂时不可用和无服务三个状态直接切换时触发此函数)"+&//& & & & arg0+" "+arg1+" "+arg2);&//& &
}&//& & });&
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);&& if (currentLocation == null)& & currentLocation = locationManager& & & .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);& }& /**&
* 开始解析&
*/& public void start() {&
if(currentLocation!=null){& & new Thread(){& &
public void run(){& & & String temp=reverseGeocode(currentLocation);& & & if(temp!=null&&temp.length()&=2)& & &
}& & }.start();&
}else{& & System.out.println("GetCity.start()未获得location");&
}& }& & /**&
* 获得城市 &
* @return&
*/& public String getCity(){&& }& & /**&
* 通过Google& map api 解析出城市&
* @param loc&
* @return&
*/& public String reverseGeocode(Location loc) {&
// /maps/geo?q=40..961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key&
String localityName = "";&
HttpURLConnection connection =&
URL serverAddress =&& try {& & // build the URL using the latitude & longitude you want to lookup& & // NOTE: I chose XML return format here but you can choose something& & // else& & serverAddress = new URL("/maps/geo?q="& & & + Double.toString(loc.getLatitude()) + ","& & & + Double.toString(loc.getLongitude())& & & + "&output=xml&oe=utf8&sensor=true&key="& & & + GOOGLE_MAPS_API_KEY);& & // set up out communications stuff& & connection =&&
// Set up the initial connection& & connection = (HttpURLConnection) serverAddress.openConnection();& & connection.setRequestMethod("GET");& & connection.setDoOutput(true);& & connection.setReadTimeout(10000);&&
connection.connect();&&
InputStreamReader isr = new InputStreamReader(connection& & &
.getInputStream());& &
InputSource source = new InputSource(isr);& &
SAXParserFactory factory = SAXParserFactory.newInstance();& &
SAXParser parser = factory.newSAXParser();& &
XMLReader xr = parser.getXMLReader();& &
GoogleReverseGeocodeXmlHandler handler = new GoogleReverseGeocodeXmlHandler();&& & xr.setContentHandler(handler);& &
xr.parse(source);&& & localityName = handler.getLocalityName();& &
System.out.println("GetCity.reverseGeocode()"+localityName);& & } catch (Exception ex) {& &
ex.printStackTrace();& & }&
} catch (Exception ex) {& & ex.printStackTrace();& & System.out.println("GetCity.reverseGeocode()"+ex);&
}&& return localityN& }&&/**&
* The final piece of this puzzle is parsing the xml tha
更多Android相关信息见 专题页面
相关资讯 & & &
& (04/17/:14)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法??
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款

我要回帖

更多关于 android 4.4.4 的文章

 

随机推荐