gpt4 book ai didi

android - 动画显示用户位置 (Android)

转载 作者:行者123 更新时间:2023-11-29 18:11:36 25 4
gpt4 key购买 nike

我正在开发一个项目,该项目具有显示(动画)当前用户位置和一个硬编码位置(给定纬度和经度)的一项功能。到目前为止,我的硬编码位置可以正常工作,但我不知道如何获取用户位置。我试过位置监听器,但我的应用程序在运行后一直崩溃。有人对此有什么建议吗?非常感激。这是代码:

 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

}


}

最佳答案

如果您使用的是 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,那么在 Activity 中:

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);

}

关于android - 动画显示用户位置 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com