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 中使用 Fragment 中的地图视图构建应用程序。在活动类中初始化 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 回答