1

myLocationOverlay を使用して地図上に現在の位置を表示する必要がある MapActivity があり、サービスから位置を取得します。

ここに私の活動があります:

public class ShowMapActivity extends MapActivity {

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlayUtility itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
private double latitude;
private double longitude;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_showmap);
    Intent serviceIntent = new Intent(this, LocationService.class);
    serviceIntent.setAction("startListening");
    startService(new Intent(this, LocationService.class));
    // check if the GPS is on
    // isGPSEnable();
    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_MAP);
    registerReceiver(updateReceiver, filter);

    // Configure the Map
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(true);
    mapController = mapView.getController();
    mapController.setZoom(14);

    myLocationOverlay = new MyLocationOverlay(this, mapView);
    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mapView.getController().animateTo(
                    myLocationOverlay.getMyLocation());
        }
    });

}

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

private final String UPDATE_MAP = "com.livetrekker.activities.UPDTAE_LOCATION";
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        longitude = intent.getDoubleExtra("long", 0);
        latitude = intent.getDoubleExtra("lat", 0);
        Log.e("LOCATION", "lat = " + latitude + " long = " + longitude);
    }
};}

そして、ここに私のサービスがあります:

public class LocationService extends Service implements LocationListener {

private final String UPDATE_MAP = "com.livetrekker.activities.UPDTAE_LOCATION";
private LocationManager locationManager;
private String provider;

private Location location;

@Override
public int onStartCommand(final Intent intent, final int flags,
        final int startId) {

    Log.e("Service", "start location");
    // if (intent.getAction().equals("startListening")) {
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        Log.e("LOCATION", "Provider " + provider + " has been selected");
        onLocationChanged(location);
    }

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, this);

    // }
    /*
     * else { if (intent.getAction().equals("stopListening")) {
     * locationManager.removeUpdates(this); locationManager = null; } }
     */

    return START_STICKY;

}

@Override
public IBinder onBind(final Intent intent) {
    return null;
}

@Override
public void onLocationChanged(final Location location) {
    this.location = location;
    double lng = location.getLongitude();
    double lat = location.getLatitude();
    Log.e("SERVICELOCATION", "lat : " + lat + " lng : " + lng);
    Intent updateIntent = new Intent();
    updateIntent.putExtra("long", lng);
    updateIntent.putExtra("lat", lat);
    updateIntent.setAction(UPDATE_MAP);
    getApplicationContext().sendBroadcast(updateIntent);
}

public void onProviderDisabled(final String provider) {
}

public void onProviderEnabled(final String provider) {
}

public void onStatusChanged(final String arg0, final int arg1,
        final Bundle arg2) {
}}

そのため、ロケーションマネージャーは正常に機能し、アクティビティで緯度と経度を取得できますが、myLocationOverlay は表示されません。

その方法を使用して、現在の位置を地図上に表示することはできますか?

ありがとう

編集:

問題を修正して交換します

myLocationOverlay = new MyLocationOverlay(this, mapView);
    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mapView.getController().animateTo(
                    myLocationOverlay.getMyLocation());
        }
    });

に :

 List overlays = mapView.getOverlays();
    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
    myLocationOverlay.enableMyLocation();
    overlays.add(myLocationOverlay);
4

1 に答える 1

0

このコードに従ってください:

public class GoogleMapsActivity extends MapActivity {
    public static final String TAG = "GoogleMapsActivity";
    private MapView mapView;
    private LocationManager locationManager;
    Geocoder geocoder;
    Location location;
    LocationListener locationListener;
    CountDownTimer locationtimer;
    MapController mapController;
    MapOverlay mapOverlay = new MapOverlay();

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.googlemap);
        initComponents();
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        mapController = mapView.getController();
        mapController.setZoom(16);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager == null) {
            Toast.makeText(GoogleMapsActivity.this,
                    "Location Manager Not Available", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            Toast.makeText(GoogleMapsActivity.this,
                    "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT)
                    .show();
            GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            mapController.animateTo(point, new Message());
            mapOverlay.setPointToDraw(point);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
        }
        locationListener = new LocationListener() {
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            }

            @Override
            public void onProviderEnabled(String arg0) {
            }

            @Override
            public void onProviderDisabled(String arg0) {
            }

            @Override
            public void onLocationChanged(Location l) {
                location = l;
                locationManager.removeUpdates(this);
                if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                } else {
                    double lat = l.getLatitude();
                    double lng = l.getLongitude();
                    Toast.makeText(GoogleMapsActivity.this,
                            "Location Are" + lat + ":" + lng,
                            Toast.LENGTH_SHORT).show();
                }
            }
        };
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
        locationtimer = new CountDownTimer(30000, 5000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (location != null)
                    locationtimer.cancel();
            }

            @Override
            public void onFinish() {
                if (location == null) {
                }
            }
        };
        locationtimer.start();
    }

    public MapView getMapView() {
        return this.mapView;
    }

    private void initComponents() {
        mapView = (MapView) findViewById(R.id.googleMapview);
    }

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

    class MapOverlay extends Overlay {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);

            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pingreen);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
            return true;
        }
    }

}

(または)このリンクをたどる:http ://www.javacodegeeks.com/2011/02/android-google-maps-tutorial.html

于 2012-09-20T09:45:25.027 に答える