package com.location;
// <uses-permission android:name="android.permission.INTERNET"/>
// <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
//<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
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.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button showGeroInfoBtn;
TextView showGeroInfoText;
Button showGcsInfoBtn;
TextView showGcsInfoText;
private double latGps;
private double lonGps;
private double latNet;
private double lonNet;
Location locationGps;
Location locationNet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//showGeroInfoBtn=(Button) findViewById(R.id.showGeroInfoBtn);
showGeroInfoText=(TextView) findViewById(R.id.showGcsInfoText);
showGcsInfoBtn=(Button) findViewById(R.id.showGcsInfoBtn);
showGcsInfoText=(TextView) findViewById(R.id.showGcsInfoText);
showGcsInfoBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
//gps与net同时声明,但系统只会选择一个使用,太不科学了。
LocationListener locationListenerGps=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
locationGps=location;
//空代码未删。
latGps=location.getLatitude();
lonGps=location.getLongitude();
Log.e("GGGGGGGGG", latGps+"_!!!___!!!_"+lonGps);
}
};
LocationListener locationListenerNet=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
locationNet=location;
latNet=location.getLatitude();
lonNet=location.getLongitude();
Log.e("NNNNNNNNNN", latNet+"_!!!___!!!_"+lonNet);
}
};
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 1000, 0, locationListenerGps);
//locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 1000, 0, locationListenerNet);
//locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
showGeroInfoText.setText("经度:"+latGps+";"+"纬度:"+lonGps+"Gps");
showGcsInfoText.setText("经度:"+latNet+";"+"纬度:"+lonNet+"Net");
}});
}
//屋内手机实测,network得到经纬度,gps没有,看来系统自动选的net。不知道系统是随即选的还是根据信号强弱选的。如果是前者,需要加if语句自己选择,如果是后者,那直接全写交给系统就行。此外,开启后须等待十来秒之后才会更新。
/*
if(locationGps==null){
showGcsInfoText.setText("经度:"+latNet+";"+"纬度:"+lonNet+"Net");
}else if(locationNet==null){
showGcsInfoText.setText("经度:"+latGps+";"+"纬度:"+lonGps+"Gps");
}
*/
//试过用asynctask处理,但更新Ui时报错,不懂为什么。个人猜测为execute()方法只能执行一次,后来location更新再执行出错。试试handler,如果同样不行,那我的猜测应该是错的。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
api参考 https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN&csw=1