В моей верхней половине экрана Android я показываю карту Google, а в нижней половине экрана Android я показываю TextBox.
Постановка задачи-
Поскольку в настоящее время я показываю карту Google на top half of the Android Screen
, вся карта Google отображается на Top Half of the Android Screen
, но мне нужно сфокусировать Current Location
на верхней половине экрана Android с изображением current_user.png
, которое у меня есть на моем drawable folder
. Так вот вопрос-
- Покажите
Current Location
с изображениемcurrent_user.png
в верхней половине экрана Android.
Ниже приведен мой Java-код, который я использую для отображения текущего местоположения в верхней половине экрана Android. Но это дает мне исключение, и мое приложение каждый раз принудительно закрывается. Что-то не так, что я здесь делаю?
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
fill_parent
по высотеMap
, то это не будет половина, потому что родитель означает, чтоLinearyLayout
также является макетом «fill_parent. And parent might be making
100%». Итак, попробуйте изменить высотуMapView
примерно на300dp
, чтобы просто проверить, применяется она или нет. - person Hamza Waqas   schedule 09.08.2012Try Catch
и поймайте строку исключения. - person Hamza Waqas   schedule 09.08.2012onCreate method
под блоком Try Catch, он начнет работать. И как только я уберу этот блок try catch. Он снова начал выдавать мне ошибку. Почему это так? - person arsenal   schedule 09.08.2012GPS
. Не уверен в этом, но ошибка, о которой вы упомянули, связана с неверным адресом местоположения. ПопробуйтеLog.v(point);
, потому что я сомневаюсь в ваших координатах. - person Hamza Waqas   schedule 09.08.2012