开发者

Current location on map using WIFI in android

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

My code:

public class LocationActivity extends MapActivity implements 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 lat开发者_高级运维itude, 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) {
}

}

How could i check it on emulator whether its working or not.?


For now, It is not possible to simulate it using emulator because it is simply doesn't support it. As alternative, you may put a flag constant (e.g. DEBUG=false/true) then if DEBUG=true, use the constant location otherwise use WIFI location.

Or use location provider which can support IP Address location. Which you can use as alternative if DEBUG=true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜