我对OSM Map的片段布局有疑问。我希望以全屏模式显示活动中的片段。 但是,地图仅显示在屏幕的三分之二处。
如何实现这一目标? 这样做只能编辑.xml文件还是我需要在清单或主要活动中进行任何更改?
这里是.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8.01" >
</fragment>
</LinearLayout>
这是MapFragment.java文件:
package com.example.uadclient;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Patter
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.bonuspack.overlays.ExtendedOverlayItem;
import org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble;
import org.osmdroid.bonuspack.overlays.Polyline;
import org.osmdroid.bonuspack.routing.Road;
import org.osmdroid.bonuspack.routing.RoadManager;
import org.osmdroid.bonuspack.routing.RoadNode;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.PathOverlay;
import android.app.Fragment;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.uadclient.R;
public class MapFragment extends Fragment {
private MapController mMapController;
private MapView mMapView;
private ItemizedIconOverlay<OverlayItem> myLocationOverlay;
protected ItemizedOverlayWithBubble<ExtendedOverlayItem> roadNodes;
protected PathOverlay roadOverlay;
FileInputStream fis = null;
private ResourceProxy mResourceProxy;
private LocationManager mLocMgr;
String s = "";
String s1 = "";
String s2 = "";
String lon = "";
String lat = "";
int mIncr = 10000;
int counter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mResourceProxy = new DefaultResourceProxyImpl(inflater.getContext().getApplicationContext());
mMapView = new MapView(inflater.getContext(), 256, mResourceProxy);
//read GPS data from file
readfromFile();
readfromFile2();
readfromFile();
String str1;
str1 = s1.toString();
readfromFile2();
String str2;
str2 = s2.toString();
try {
countLines();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lon = str1;
lat = str2;
while (counter > 0){ //Anzahl Zeilen -> Anzahl der Punkte
double var_lon = Double.parseDouble(lon);
double var_lat = Double.parseDouble(lat);
View v = inflater.inflate(R.layout.meetfragment, null);
mMapView = (MapView) v.findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mMapController = mMapView.getController();
mMapController.setZoom(13);
GeoPoint point1 = new GeoPoint(var_lat, var_lon);
GeoPoint point2 = new GeoPoint(52.120310618246236, 9.905589604799236); // centre map
GeoPoint point3 = new GeoPoint(52.11712212336459, 9.899281049195961);
mMapController.setCenter(point1);
//mMapController.setCenter(point3);
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>(3);
// Put overlay icon on the centre
items.add(new OverlayItem("Here", "SampleDescription", point1));
/* items.add(new OverlayItem("Here", "SampleDescription", point2));
items.add(new OverlayItem("Here", "SampleDescription", point3));*/
ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>(3);
waypoints.add(point1);
waypoints.add(point2);
waypoints.add(point3);
// new UpdateRoadTask().execute(waypoints);
if (savedInstanceState == null){
//Test road service at first launch:
getRoadAsync(point1, point2);
} else {
Road mRoad = null;
updateUIWithRoad(mRoad);
}
this.myLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
return true;
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.myLocationOverlay);
counter--;
return v;
}
return container;}
public void getRoadAsync(GeoPoint start, GeoPoint destination){
ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>(2);
waypoints.add(start);
//intermediate waypoints can be added here:
//waypoints.add(new GeoPoint(48.226, -1.9456));
waypoints.add(destination);
new UpdateRoadTask().execute(waypoints);
}
void updateUIWithRoad(Road road){
List<Overlay> mapOverlays = mMapView.getOverlays();
if (roadOverlay != null){
mapOverlays.remove(roadOverlay);
}
if (road.mStatus == Road.STATUS_DEFAULT)
Toast.makeText(mMapView.getContext(), "We have a problem to get the route", Toast.LENGTH_SHORT).show();
roadOverlay = RoadManager.buildRoadOverlay(road, mMapView.getContext());
Overlay removedOverlay = mapOverlays.set(1, roadOverlay);
//we set the road overlay at the "bottom", just above the MapEventsOverlay,
//to avoid covering the other overlays.
mapOverlays.add(removedOverlay);
putRoadNodes(road);
mMapView.invalidate();
}
private void putRoadNodes(Road road){
roadNodes.removeAllItems();
Drawable marker = getResources().getDrawable(R.drawable.marker_node);
int n = road.mNodes.size();
TypedArray iconIds = getResources().obtainTypedArray(R.array.direction_icons);
for (int i=0; i<n; i++){
RoadNode node = road.mNodes.get(i);
String instructions = (node.mInstructions==null ? "" : node.mInstructions);
ExtendedOverlayItem nodeMarker = new ExtendedOverlayItem(
"Step " + (i+1), instructions,
node.mLocation, this);
nodeMarker.setSubDescription(road.getLengthDurationText(node.mLength, node.mDuration));
nodeMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER);
nodeMarker.setMarker(marker);
int iconId = iconIds.getResourceId(node.mManeuverType, R.drawable.ic_empty);
if (iconId != R.drawable.ic_empty){
Drawable icon = getResources().getDrawable(iconId);
nodeMarker.setImage(icon);
}
roadNodes.addItem(nodeMarker);
}
}
/**
* Async task to get the road in a separate thread.
*/
private class UpdateRoadTask extends AsyncTask<Object, Void, Road> {
protected Road doInBackground(Object... params) {
@SuppressWarnings("unchecked")
ArrayList<GeoPoint> waypoints = (ArrayList<GeoPoint>)params[0];
//RoadManager roadManager = new GoogleRoadManager();
RoadManager roadManager = new OSRMRoadManager();
/*
RoadManager roadManager = new MapQuestRoadManager();
Locale locale = Locale.getDefault();
roadManager.addRequestOption("locale="+locale.getLanguage()+"_"+locale.getCountry());
*/
return roadManager.getRoad(waypoints);
}
private void putRoadNodes(Road road){
ItemizedOverlayWithBubble<ExtendedOverlayItem> roadNodes = null;
roadNodes.removeAllItems();
Drawable marker = getResources().getDrawable(R.drawable.marker_node);
int n = road.mNodes.size();
TypedArray iconIds = getResources().obtainTypedArray(R.array.direction_icons);
for (int i=0; i<n; i++){
RoadNode node = road.mNodes.get(i);
String instructions = (node.mInstructions==null ? "" : node.mInstructions);
ExtendedOverlayItem nodeMarker = new ExtendedOverlayItem(
"Step " + (i+1), instructions,
node.mLocation, this);
nodeMarker.setSubDescription(road.getLengthDurationText(node.mLength, node.mDuration));
nodeMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER);
nodeMarker.setMarker(marker);
int iconId = iconIds.getResourceId(node.mManeuverType, R.drawable.ic_empty);
if (iconId != R.drawable.ic_empty){
Drawable icon = getResources().getDrawable(iconId);
nodeMarker.setImage(icon);
}
roadNodes.addItem(nodeMarker);
}
}
}
public void onFinish(Bundle savedInstanceState) {
super.getActivity().finish();
}
public void onDestroy() {
super.onDestroy();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public int countLines() throws IOException {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/GPSdata.txt");
InputStream is = new BufferedInputStream(new FileInputStream(file));
try {
byte[] c = new byte[1024];
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++counter;
}
}
}
return (counter == 0 && !empty) ? 1 : counter;
} finally {
is.close();
}
}
public String readfromFile() {
// Read from file
BufferedReader bufferedReader = null;
// Pattern being checked for lattidude
Pattern p1 = Pattern.compile("[0-9]{1}.[0-9]+");
Matcher m;
// Der Pfad zur Textdatei
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/GPSdata.txt");
try {
// Der BufferedReader erwartet einen FileReader.
// Diesen kann man im Konstruktoraufruf erzeugen.
bufferedReader = new BufferedReader(new FileReader(file));
String line;
String str = null;
// null wird bei EOF oder Fehler zurueckgegeben
while ((line = bufferedReader.readLine()) != null) {
// Zeile auf der Konsole ausgeben
// Pattern currently being checked for
m = p1.matcher(line);
if (m.find()) {
str = m.group();
// arrayList1.add(str);
}
System.out.println(line);
s1 = str;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s1;
}
public String readfromFile2() {
// Read from file
BufferedReader bufferedReader = null;
// Pattern being checked for longitude
Pattern p = Pattern.compile("[0-9]+.[0-9]+");
Matcher m;
// Der Pfad zur Textdatei
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/GPSdata.txt");
try {
// Der BufferedReader erwartet einen FileReader.
// Diesen kann man im Konstruktoraufruf erzeugen.
bufferedReader = new BufferedReader(new FileReader(file));
String line;
String str1 = null;
// null wird bei EOF oder Fehler zurueckgegeben
while ((line = bufferedReader.readLine()) != null) {
// Zeile auf der Konsole ausgeben
// Pattern currently being checked for
m = p.matcher(line);
while (m.find()) {
str1 = m.group();
//arrayList2.add(str1);
}
System.out.println(line);
s2 = str1;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s2;
}
/*public void readLine(){
try {
BufferedReader in = new BufferedReader(new FileReader("GPSdata.txt"));
String zeile = null;
while ((zeile = in.readLine()) != null){
System.out.println("Gelesene Zeile:" + zeile);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}*/
}
这是mainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uadclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<permission
android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.bosch.uadclient.MainMenuActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.bosch.uadclient.LocationProviderService" >
</service>
</application>
</manifest>
祝福