【问题标题】:Show Current Location with an Image on Top Half of the Android Screen在 Android 屏幕的上半部分用图像显示当前位置
【发布时间】:2012-08-09 06:58:13
【问题描述】:

在 Android 屏幕的上半部分,我正在显示 Google 地图,在 Android 屏幕的下半部分,我正在显示 TextBox。

问题陈述-

目前我在top half of the Android Screen 上显示谷歌地图,所以整个谷歌地图都显示在Top Half of the Android Screen 上,但我需要将我的Current Location 集中在Android 屏幕的上半部分,图像为@987654324 @ 我在我的drawable folder 中。所以这里的问题是 -

  1. 在 Android 屏幕的上半部分显示 Current Location 和图像 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;
    }
}

}

下面是我的 XML 文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.google.android.maps.MapView 
        android:id="@+id/mapView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:apiKey="0vAX8Xe9xjo5gkFNEEIH7KdHkNZNJWNnsjUPKkQ" 
        android:clickable="true" 
        android:enabled="true" /> 

    <TextView 
        android:id="@+id/textView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:text="TextView" /> 

</LinearLayout> 

我得到的异常-

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.circlemapandroid/com.example.circlemapandroid.MainActivity}: 
java.lang.IllegalArgumentException: requested provider network doesn't exisit

【问题讨论】:

  • 如果你在Map 的高度中有fill_parent,那么它不会是一半,因为父级意味着LinearyLayout 也是'fill_parent. And parent might be making 100%` 布局。所以,试着改变MapView 的高度,比如300dp 来测试它是否应用。
  • 但问题是,它甚至还没有开始。而且每次都被强制关闭。
  • Try Catch 包裹你的代码,并捕获查看异常行。
  • 当服务在设置>位置和安全>通过网络定位时被禁用
  • @Rajeshwaran.T 这是什么意思,你能详细说明一下吗?

标签: java android google-maps android-mapview locationmanager


【解决方案1】:

您必须像使用 GPS 提供商一样检查网络提供商是否已启用:

if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
    locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
}

您的清单中还必须存在以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

【讨论】:

  • 让我试试那个网络提供商的东西,但是我的清单文件中已经存在权限设置,我发现很奇怪的东西——如果我将所有内容都包装在 Try Catch Block 下的 onCreate 方法中,它就开始工作了.一旦我删除了 try catch 块。它再次开始给我错误。为什么会这样?
  • 尝试注册网络监听器时出现异常,因为在 Android 设置中网络位置提供程序已关闭。当然,当您使用 try-catch 捕获异常时,异常就消失了,但这并不意味着问题已解决。你只是忽略了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2012-03-18
相关资源
最近更新 更多