1

私が欲しいのは、GPS がオフラインの場合に表示される GUI で、ユーザーは現在の住所を入力できます。

Google マップを使用して Eclipse で Android アプリを構築しています。アクティビティの 1 つを開くと、マップビューが表示されます。私がやりたいのは、マップをロードする代わりに GPS がオンになっていない場合、ユーザーが現在の場所の住所を入力できる EditText テキスト ビューを表示するアクティビティが必要な場合です。入力後にマップ ビューが表示されます。この住所。GPS ブール値を取得して GPS がオンかどうかを表示するロジックはすべてありますが、実際に必要なのは、Activity.java と、GPS に基づいてこれらの GUI 項目のいずれかを表示するレイアウト xml ロジックです。したがって、GPS がマップビューにある場合は表示され、GPS がアクティビティにない場合は住所とボタンを入力するエリアが表示され、ボタンがクリックされると、このロジックはユーザーが入力したマップビュー上の場所を表示します.

Activity.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) {
    }
      }

  }

activity_map.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 を変更する方法はありますか?

4

2 に答える 2