package com.example.gps;
import java.util.List;
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.provider.Settings;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
Location location;
private Button btn;
// String placename = "";
public static final String action = "jason.broadcast.action";
public static String placename ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
openGPSSettings();
String str=getLocation();
}
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("维度:" + latitude + "\n经度" + longitude);
} else {
tv1.setText("无法获取地理信息");
}
}
public String getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
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); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
String placename=updateWithNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
locationListener);
return placename;
}
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
}
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 String updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.myLocationText);
// try {
// 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 = null;
try {
// Thread.sleep(2000);
places = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 5);
// Thread.sleep(2000);
Toast.makeText(MainActivity.this, places.size() + "",
Toast.LENGTH_LONG).show();
System.out.println(places.size() + "");
} catch (Exception e) {
e.printStackTrace();
}
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) + ", "
+ ((Address) places.get(0)).getAddressLine(1);
// 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经度:" + lng;
myLocationText.setText("您当前的位置是:\n" + placename);
} else {
latLongString = "无法获取地理信息";
myLocationText.setText("您当前的位置是:\n" + latLongString);
}
Log.d("test", placename);
return placename;
}
}
import java.util.List;
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.provider.Settings;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
Location location;
private Button btn;
// String placename = "";
public static final String action = "jason.broadcast.action";
public static String placename ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
openGPSSettings();
String str=getLocation();
}
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("维度:" + latitude + "\n经度" + longitude);
} else {
tv1.setText("无法获取地理信息");
}
}
public String getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
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); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
String placename=updateWithNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
locationListener);
return placename;
}
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
}
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 String updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.myLocationText);
// try {
// 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 = null;
try {
// Thread.sleep(2000);
places = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 5);
// Thread.sleep(2000);
Toast.makeText(MainActivity.this, places.size() + "",
Toast.LENGTH_LONG).show();
System.out.println(places.size() + "");
} catch (Exception e) {
e.printStackTrace();
}
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) + ", "
+ ((Address) places.get(0)).getAddressLine(1);
// 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经度:" + lng;
myLocationText.setText("您当前的位置是:\n" + placename);
} else {
latLongString = "无法获取地理信息";
myLocationText.setText("您当前的位置是:\n" + latLongString);
}
Log.d("test", placename);
return placename;
}
}