Java 用安卓屏幕上半部分的图像显示当前位置
在Android屏幕的上半部分,我显示谷歌地图,在Android屏幕的下半部分,我显示文本框 问题陈述- 目前,我正在Android屏幕的上半部分显示google地图,因此,整个谷歌地图都显示在Android屏幕的上半部分,但我需要将我的Java 用安卓屏幕上半部分的图像显示当前位置,java,android,google-maps,android-mapview,locationmanager,Java,Android,Google Maps,Android Mapview,Locationmanager,在Android屏幕的上半部分,我显示谷歌地图,在Android屏幕的下半部分,我显示文本框 问题陈述- 目前,我正在Android屏幕的上半部分显示google地图,因此,整个谷歌地图都显示在Android屏幕的上半部分,但我需要将我的当前位置集中在Android屏幕的上半部分,使用我的可绘图文件夹中的图像Current_user.png。所以这里的问题是—— 在Android屏幕的上半部分显示带有图像Current_user.png的当前位置 下面是我用来在Android屏幕上半部分显示当前
当前位置
集中在Android屏幕的上半部分,使用我的可绘图文件夹中的图像Current_user.png
。所以这里的问题是——
在Android屏幕的上半部分显示带有图像Current_user.png
的当前位置
下面是我用来在Android屏幕上半部分显示当前位置的Java代码。但它给了我一个例外,我的应用程序每次都被强制关闭。我在这里做错什么了吗
public class MainActivity 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();
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
initComponents();
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(16);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager == null) {
Toast.makeText(getApplicationContext(),
"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(getApplicationContext(),
"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(getApplicationContext(),
"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.mapView);
}
@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.current_user);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
return true;
}
}
您必须检查网络提供商是否像使用GPS提供商一样启用:
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
}
您的舱单中还必须有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
如果在地图的高度中有填充父项
,则不会是一半,因为父项的意思是线性布局
也是“填充父项”。家长可能正在做
100%的布局。因此,尝试更改MapView
的高度,例如300dp
,以测试它是否正在应用。但问题是,它甚至还没有开始。每次它都会被强制关闭。请使用Try Catch
包装代码,并捕获异常。当服务在“设置”>“位置和安全性”>“位置通过”中被禁用时,请查看异常行network@Rajeshwaran.T这意味着什么,你能详细说明一下吗?让我试试网络提供商的东西,但权限设置已经存在于我的清单文件中,我发现了一件非常奇怪的事情——如果我将所有内容都封装在Try-Catch块下的onCreate方法中,它就会开始工作。我一移开那个挡块,就试试看。它又开始给我错误了。为什么会这样?当您尝试注册网络侦听器时会出现异常,因为在Android设置中,网络位置提供程序已关闭。当然,当您使用try-catch捕获异常时,异常就消失了,但这并不意味着问题已经解决。你只是忽略了它。
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>