使用Android中的WIFI在地图上的当前位置

时间:2011-06-05 10:28:51

标签: android-maps

i just need to find the current location on maps using WIFI.I used tha below code to do that.

我的代码:

公共类LocationActivity扩展MapActivity实现LocationListener {

private MapView mapView;
private LocationManager locationManager;
private String latitude,longtitude;

@Override
protected boolean isRouteDisplayed() {
    return false;
}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Bundle bundle = this.getIntent().getExtras();
    latitude = bundle.getString("latitude");
    longtitude = bundle.getString("longtitude");
    setContentView(R.layout.locationtab);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    locationIdentifier();

}




private void createMyLocOverlay(Double latitude, Double longtitude) {

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(
            R.drawable.mylocation);

    GeoPoint point = new GeoPoint((int) (latitude * 1E6),
            (int) (longtitude * 1E6));
    OverlayItem overlayitem = new OverlayItem(point, null, "You are here!");
    MyLocationOverlay itemizedoverlay = new MyLocationOverlay(drawable,
            this);
    itemizedoverlay.addOverlay(overlayitem);
    MyLocationOverlay overlayToRemove = null;
    for (Overlay overlay : mapOverlays) {
        if (overlay instanceof MyLocationOverlay) {
            overlayToRemove = (MyLocationOverlay) overlay;
        }
    }
    if (overlayToRemove != null) {
        mapOverlays.remove(overlayToRemove);
    }
    mapOverlays.add(itemizedoverlay);
}

public void locationIdentifier() {


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    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); 
       Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) {
        createMyLocOverlay(location.getLatitude(), location.getLongitude());
        Toast.makeText(
                this,
                "Latitude : " + location.getLatitude() + " : Longtitude : "
                        + location.getLongitude(), Toast.LENGTH_LONG)
                .show();
    } else {
        Toast.makeText(this, "Latitude/Longtitude not found",
                Toast.LENGTH_LONG).show();
    }
}

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        createMyLocOverlay(location.getLatitude(), location.getLongitude());
        Toast.makeText(
                this,
                "LocationChanged Latitude : " + location.getLatitude()
                        + " Longtitude : " + location.getLongitude(),
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Location is null", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_LONG)
            .show();

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_LONG).show();
}

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

}

我如何在模拟器上检查它是否有效。?

1 个答案:

答案 0 :(得分:0)

目前,无法使用模拟器进行模拟,因为它根本不支持它。作为替代方案,您可以将标志设置为常量(例如DEBUG = false / true),然后如果DEBUG = true,则使用常量位置,否则使用WIFI位置。

或者使用可以支持IP地址位置的位置提供程序。如果DEBUG = true,您可以使用它作为替代。