1

このコードはネットワークで完璧に機能しますが、WiFiテストGPSwifiを使用する必要があるため、GPSwifiとネットワークの両方をチェックしたいと思います。Androidアプリでこの作業を行う方法を助けることはできません。

    public class Map extends MapActivity {
        double longitude;
        double latitude;
        MapView mapView;
        private LocationManager mlocManager;
        private LocationListener mlocListener;
        private boolean gp;
        Geocoder gd;
        GeoPoint point;
        List<Overlay> mapOverlays;
        CustomItemizedOverlay itemizedOverlay;
        MapController mapController;
        private SportingSoccerApplication soccerApplication;
        OverlayItem overlayitem;
        SportingSoccerApplication soccerApplication2;
        Drawable drawable;
        RelativeLayout rv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.map);

            rv = (RelativeLayout) findViewById(R.id.relativeLayout1);
            mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            mlocListener = new MyLocationListener();
            gp = mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            //Log.v("log_tag", gp + "");
            //if (!gp == true) {
            //  turnGPSOn();
            //}


            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    mapView.setBuiltInZoomControls(true);

                }
            });

            mapView.setBuiltInZoomControls(true);

            mapOverlays = mapView.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.markerdark);
            itemizedOverlay = new CustomItemizedOverlay(drawable, this);

                point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
                overlayitem = new OverlayItem(point, "", toString());
                itemizedOverlay.addOverlay(overlayitem);
                mapOverlays.add(itemizedOverlay);
                mapController = mapView.getController();
                mapController.animateTo(point);

                mapController.setZoom(14);

                soccerApplication = (SportingSoccerApplication) getApplication();
        }



        @Override
        protected void onResume() {
            super.onResume();
            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                    mlocListener);

        }



        public class MyLocationListener implements LocationListener {

            public void onLocationChanged(Location loc) {

                mapView.getOverlays().clear();
                mapView.postInvalidate();
                mapOverlays.clear();
                gd = new Geocoder(getApplicationContext(), Locale.getDefault());
                longitude = loc.getLongitude();
                latitude = loc.getLatitude();
                Log.i("log_tag",longitude+"*******"+latitude);

                /*try {
                    List<Address> place = gd
                            .getFromLocation(latitude, longitude, 1);
                    if(place.size() > 0) {
                     place.get(0).getLocality();
                    }
                    // Toast.makeText(getApplicationContext(), bus_no, 2).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                point = new GeoPoint((int) (latitude * 1E6),
                        (int) (longitude * 1E6));

                Log.i("log_tag",latitude+"----"+longitude);
                overlayitem = new OverlayItem(point, "", toString());
                itemizedOverlay.addOverlay(overlayitem);
                mapOverlays.add(itemizedOverlay);
                mapController.animateTo(point);
                Log.v("log_tag", "u" + latitude + longitude + "");
                soccerApplication.setLatitude(latitude);
                soccerApplication.setLongitude(longitude);

            }

            public void onProviderDisabled(String provider) {

            }

            public void onProviderEnabled(String provider) {

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {

            }
        }



        protected boolean isRouteDisplayed() {
            return false;
        }

    }

このコードはAndroidネットワークベースで動作しますが、GPSwifiで動作する必要があります。

                <?xml version="1.0" encoding="utf-8"?>
                <com.google.android.maps.MapView
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/mapview"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:clickable="true"
                        android:apiKey="0bP2YJLhub2rGpXpOtkWFqcV3Qm5SS11Bl-cFyQ"
                />

            package com.example.gpstest;

    import java.util.ArrayList;

    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;

    import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;

    public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
        private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
        private Context mcontext;

        public CustomItemizedOverlay(Drawable defaultMarker) {

            super(boundCenterBottom(defaultMarker));

            // TODO Auto-generated constructor stub
        }

        public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
            super(boundCenterBottom(defaultMarker));
            mcontext = context;
        }

        @Override
        protected OverlayItem createItem(int i) {
            // TODO Auto-generated method stub
            return mapOverlays.get(i);
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return mapOverlays.size();
        }

        @Override
        protected boolean onTap(int index) {
            OverlayItem item = mapOverlays.get(index);
            AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);
            dialog.setTitle(item.getTitle());
            dialog.setMessage(item.getSnippet());
            dialog.show();
            return true;
        }

        public void addOverlay(OverlayItem overlay) {
            mapOverlays.add(overlay);
            populate();
        }

    }
4

4 に答える 4

1

あなたは1つのことをすることができます..ちょうどあなたのWiFiを自動的に開始しますアクティビティ開始時に

 protected void onStart() {
         // TODO Auto-generated method stub
         super.onStart();

           WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
             if (wifiManager != null) {

             wifiManager.setWifiEnabled(true);
             }
        } 

マニフェストファイルにも権限を入れてください:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"  />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
于 2012-12-03T07:17:39.270 に答える
0

これはどうですか:http ://sourceforge.net/projects/libwlocate/

このライブラリは、単一のインターフェイスを介してGPSとWLANの測位を実行し、使いやすいです。もちろん、Androidのソースとポートも利用できます。

于 2012-12-03T07:03:28.040 に答える
0

先着順で両方を使用したい場合は、wifiとネットワークの両方に2つの別々のロケーションリスナーを作成できます。以下のように。

try
        {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }catch(Exception ex){
            ex.printStackTrace();
        }

    //don't start listeners if no provider is enabled
    if(!gps_enabled && !network_enabled)
        return false;

    if(gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    if(network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

そして、あなたは両方のリスナーjusを聞くことができます

LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            //          timer1.cancel();
            Log.d("from", "GPS");
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        //          timer1.cancel();
        Log.d("from", "NETWORK");
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

これが、先着順のリスナーを作成する方法です。この助けを願っています。

于 2012-12-03T07:15:09.227 に答える
0

このコードを試してください..Network_Providerで動作します

protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    boolean answer;
    answer=isOnline();
    System.out.println(answer);

    if(answer){
    LocationManager L = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if (L.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    gps_loc();
    } else
    {       
    showSettingsAlert();        
    }
}

else
{
    Toast.makeText(getApplicationContext(), "Check your Internet Connection",Toast.LENGTH_SHORT).show();    
}

}

public boolean isOnline(){

ConnectivityManager cm =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    return true;

}
return false;

}

public void showSettingsAlert(){
    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("GPS")
    .setMessage("GPS is not enabled. Do you want to go to settings menu?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
    public void onClick(DialogInterface dialog, int which) {
        Intent I = new Intent( 
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
            startActivity(I); 
            Toast.makeText(getApplicationContext(),"Tick Use_wifi settings and click back button", Toast.LENGTH_LONG).show();

    }

})
.setNegativeButton("No", null)
.show();
}
void gps_loc()
{
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//  LocationListener ll = new mylocationlistener();
    Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
     if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Toast.makeText(getApplicationContext(),"", Toast.LENGTH_SHORT).show();
              double lat=location.getLatitude();
              double lng=location.getLongitude();
              List<Address> addresses = null;
                try {
                    addresses = new Geocoder(getApplicationContext(),Locale.getDefault()).getFromLocation(lat,lng, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Address addr= addresses.get(0);

              Country=addr.getCountryName();
               City=addr.getLocality();
               area=addr.getFeatureName();
               pincode=addr.getPostalCode();
                currentloc=area+" ,"+City+" ,"+Country;

                Toast.makeText(getApplicationContext(),currentloc, Toast.LENGTH_SHORT).show();

            }  
}
于 2012-12-03T07:48:53.690 に答える