1

現在のユーザーの場所を表示する (アニメーション化する) 1 つの機能と、1 つのハードコードされた場所 (指定された緯度と経度) を持つプロジェクトに取り組んでいます。これまでのところ、ハードコーディングされた場所が機能していますが、ユーザーの場所を取得する方法がわかりません。ロケーション リスナーを試しましたが、実行後にアプリケーションがクラッシュし続けます。誰かアドバイスはありますか?とても有難い。コードは次のとおりです。

 public class LocationsActivity extends MapActivity implements LocationListener {

MapView mapView;
MapController mc;
GeoPoint p;
LocationListener locListener;



public void onCreate(Bundle savedInstanceState) {

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

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

    mc = mapView.getController();
    String coordinates[] = {"52.67596","-8.64910"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
    OverlayItem overlayitem = new OverlayItem(p, "Limerick Institute of 
        Technology", "Moylish");




    mc.animateTo(p);
    mc.setZoom(17);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, 
        this);
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
    mapView.invalidate();
}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}


public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}


public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}


public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


 }
4

2 に答える 2

3

MapActivity を使用している場合は、次のコードを使用できます。

/**
 * Set current location using MyLocationOverlay
 */
public void setCurrentLocation() {

    myLocationOverlay.runOnFirstFix(new Runnable() {
        @Override
        public void run() {

            if (null != myLocationOverlay.getMyLocation()) {

                /**
                 * Put extra sleep to avoid concurrentModicationException
                 */
                try {
                    Thread.sleep(1000);

                } catch (Exception e) {
                    // TODO: handle exception
                }
                map.getController().animateTo(
                        myLocationOverlay.getMyLocation());

                map.getOverlays().add(myLocationOverlay);

            }

        }
    });

}

MapFragment を使用している場合、アクティビティ内:

1. mapFragment.getMap().setMyLocationEnabled(true);

それから電話する

    private void moveMapToMyLocation() {

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

    Criteria crit = new Criteria();

    Location loc = locMan.getLastKnownLocation(locMan.getBestProvider(crit,
            false));

    CameraPosition camPos = new CameraPosition.Builder()

    .target(new LatLng(loc.getLatitude(), loc.getLongitude()))

    .zoom(12.8f)

    .build();

    CameraUpdate camUpdate = CameraUpdateFactory.newCameraPosition(camPos);

    mapFragment.getMap().moveCamera(camUpdate);

}
于 2012-12-31T12:32:48.077 に答える
0

フラグメント内のマップビューを使用して、ICS でアプリを構築しています。アクティビティ クラスで mapView を初期化した後、マップを表示するために使用されるフラグメントである mapFragment に渡します。

これは私の活動クラスのコードの一部です:

mapView = new MapView(this, getString(R.string.mapsAPI));

myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(this.myLocationOverlay);
mapView.postInvalidate();

public void calculateLocation() {
setLocationManager();
setLocation();
setGeoPoint(location);
}

public void setGeoPoint(Location location) {
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();
  geoPoint = new GeoPoint((int)( latitude * 1E6), (int)( longitude * 1E6 ));
}

public void setLocation() {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

public void setLocationManager() {
locationManager = ((LocationManager)getSystemService(Context.LOCATION_SERVICE));
}

これは私のmapFragmentクラスのコードです:

public class MapFragment extends Fragment
{
  MapView gMap = null;
  GeoPoint gPoint = null;
  Location location = null;

  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
    gMap = ((NFCDemoActivity)getActivity()).getMapView();
    return this.gMap;
  }

  public void onActivityCreated(Bundle paramBundle) {
    super.onActivityCreated(paramBundle);
    ((NFCDemoActivity)getActivity()).calculateLocation();
    gPoint = ((NFCDemoActivity)getActivity()).getGeoPoint();
    gMap.setClickable(true);
    gMap.setBuiltInZoomControls(true);
    gMap.getController().setZoom(18);
    gMap.getController().setCenter(gPoint);
  }

  public void onDestroy() {
    super.onDestroy();
  }
}

これにより、ユーザーの現在地がわかります。

于 2012-04-28T18:31:52.647 に答える