如果 GPS 离线,我想要一个 GUI,以便用户可以输入当前地址。
我正在使用 Google 地图在 Eclipse 中构建一个 Android 应用程序。我的一项活动在打开时会显示一个地图视图。我想做的是如果 GPS 没有打开而不是加载地图我希望活动显示一个 EditText 文本视图,用户可以在其中输入他们当前位置的地址,然后地图视图会在输入后显示这个地址。我拥有所有逻辑来获取 GPS 布尔值以显示 GPS 是否打开,但我真正需要的是 Activity.java 和布局 xml 逻辑,它们将基于 GPS 开启显示这些 GUI 项目中的任何一个。因此,如果 GPS 在地图视图上将显示,如果 GPS 不在活动上,将显示一个区域以输入地址和一个按钮,一旦单击该按钮,此逻辑将在地图视图上显示用户输入的位置.
活动.java 文件
@SuppressLint("ShowToast")
public class MapViewActivity extends FragmentActivity {
static LatLng currentLatLng;
LatLng currentLocation;
private GoogleMap map;
boolean firstPass = true;
double currentLat;
double currentlong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_view);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
showCurrentLocationOnMap();
try {
showSheltersAndFuelStops();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//This will change when SQL connection string is implemented.
private void showSheltersAndFuelStops() throws IOException {
List<McDonalds> mcdList = new ArrayList<McDonalds>();
List<Subway> subwayList = new ArrayList<Subway>();
String subwayRoswell1 = "1307 N Main St, Roswell, NM, 88203";
String subwayRoswell2 = "1701 W Second St, Roswell, NM, 88201";
String subwayLC1 = "2001 E Lohman, Las Cruces, NM, 88001";
String subwayLC2 = "2821 N Telshor, Las Cruces, NM 88011";
String mcdonaldsRoswell1 = "720 N Main St, Roswell, NM, 88201";
String mcdonaldsRoswell2 = "1804 S Main St, Roswell, NM, 88203";
String mcdonaldsLC1 = "4810 Mesa Grande, Las Cruces, NM, 88012";
String mcdonaldsLC2 = "571 Walton Blvd, Las Cruces, NM, 88001";
Subway subway = new Subway(" 1307 N Main St, Roswell, NM, 88203", 1.58, 8.0);
subwayList.add(subway);
Subway subway1 = new Subway(" 1701 W Second St, Roswell, NM, 88201", 2.47, 12.0);
subwayList.add(subway1);
Subway subsLC = new Subway(" 2001 E Lohman, Las Cruces, NM, 88001", 147.99, 185.0);
subwayList.add(subsLC);
Subway subsLC1 = new Subway(" 2821 N Telshor Drive, Las Cruces, NM, 88011", 147.12, 200.0);
subwayList.add(subsLC1);
//This will be replaced by a Read Only Stored procedure that gets the List of Shelters
McDonalds mcdonalds = new McDonalds(" 720 N Main St, Roswell, NM, 88201", 1.08, 8.0);
mcdList.add(mcdonalds);
McDonalds mcdonalds1 = new McDonalds(" 1804 S Main St, Roswell, NM, 88203", 2.9, 12.0);
mcdList.add(mcdonalds1);
McDonalds mcdonaldslc = new McDonalds(" 4810 Mesa Grande, Las Cruces, NM, 88012", 143.2, 185.0);
mcdList.add(mcdonaldslc);
McDonalds mcdonaldslc1 = new McDonalds(" 571 Walton Blvd, Las Cruces, NM, 88001", 147.62, 200.0);
mcdList.add(mcdonaldslc1);
showFuelStops(subwayRoswell1);
showFuelStops(subwayRoswell2);
showFuelStops(subwayLC1);
showFuelStops(subwayLC2);
showShelters(mcdonaldsRoswell1);
showShelters(mcdonaldsRoswell2);
showShelters(mcdonaldsLC1);
showShelters(mcdonaldsLC2);
}
private void showFuelStops(String location) throws IOException{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.fillingstation);
currentLat= addresses.get(0).getLatitude();
currentlong= addresses.get(0).getLongitude();
LatLng subLoc = new LatLng(currentLat, currentlong);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).title("Subway, " + location));
}
}
private void showShelters(String location) throws IOException{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.shelter);
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
LatLng subLoc = new LatLng(latitude, longitude);
Marker hamburg = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).title("McDonalds, " + location));
}
}
private void showCurrentLocationOnMap(){
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
SupportMapFragment mf= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
map = mf.getMap();
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
LocationListener ll = new Mylocationlistener();
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPS){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_view, menu);
return true;
}
/**
*Mylocationlistener class will give the current GPS location
*with the help of Location Listener interface
*/
private class Mylocationlistener implements LocationListener {
private boolean zoomed = false;
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// ---Get current location latitude, longitude---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
// Zoom in, animating the camera.
if (!zoomed) {
map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
zoomed = true;
}
if (!firstPass){
currentLocationMarker.remove();
}
firstPass = false;
Toast.makeText(MapViewActivity.this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
Toast.LENGTH_LONG).show();
} else {
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
活动地图.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
基于我在 Activity.java 文件中设置的 isGPS 布尔值,我可以将其用作布尔值开关。有没有办法根据需要更改 gui?