对不起我的英文。
我正在开发有关区域检测的应用程序。 我想通过GPS检测Android设备的位置,当它在Polyline上时,它会提醒用户。所以我想将Polyline保留在本地存储中。
但我不知道如何保留它?
我的解决方案是:
错误 - 单击“风险区域”后无法运行它仅在地图上显示标记但不显示折线。它会崩溃。
MainActivity
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import android.R.color;
import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
GoogleMap mMap;
GoogleMapOptions option;
double lat=0;
double lng=0;
Direction md;
LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);
PolylineOptions rectLine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
Button buttonNormal = (Button) findViewById(R.id.button1);
buttonNormal.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
Button buttonSatellite = (Button) findViewById(R.id.button2);
buttonSatellite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});
Button buttonTerrain = (Button) findViewById(R.id.button3);
buttonTerrain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
});
Button buttonHybrid = (Button) findViewById(R.id.button4);
buttonHybrid.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
Button buttonLocate = (Button) findViewById(R.id.button7);
buttonLocate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng coordinate = new LatLng(lat, lng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
(coordinate), 15));
mMap.addMarker(new MarkerOptions()
.position(coordinate)
.title("Prove")
.snippet("Family")
.draggable(true)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.remove();
Toast.makeText(getApplicationContext(),
"Remove Marker" + String.valueOf(arg0.getId()),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
});
/*Button buttonPoly = (Button) findViewById(R.id.button6);
buttonPoly.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PolylineOptions rectLine = new PolylineOptions().add(
new LatLng(lat, lng))
.add(new LatLng(lat + 0.00001, lng + 0.0001));
mMap.addPolyline(rectLine);
}
});*/
Button buttonDest = (Button) findViewById(R.id.button5);
buttonDest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));
md=new Direction();
Document doc = md.getDocument(fromPosition, toPosition, md.MODE_DRIVING);
int duration = md.getDurationValue(doc);
String distance = md.getDistanceText(doc);
String start_address = md.getStartAddress(doc);
String copy_right = md.getCopyRights(doc);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
mMap.addPolyline(rectLine);
}
});
}
}
Direction.java
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
public class Direction {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";
public Direction(){
}
public Document getDocument(LatLng start, LatLng end, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode=driving";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getDurationText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DurationText", node2.getTextContent());
return node2.getTextContent();
}
public int getDurationValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DurationValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getDistanceText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DistanceText", node2.getTextContent());
return node2.getTextContent();
}
public int getDistanceValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DistanceValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getStartAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("start_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getEndAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("end_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getCopyRights (Document doc) {
NodeList nl1 = doc.getElementsByTagName("copyrights");
Node node1 = nl1.item(0);
Log.i("CopyRights", node1.getTextContent());
return node1.getTextContent();
}
public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
}
locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
}
}
return listGeopoints;
}
private int getNodeIndex(NodeList nl, String nodename) {
for(int i = 0 ; i < nl.getLength() ; i++) {
if(nl.item(i).getNodeName().equals(nodename))
return i;
}
return -1;
}
private ArrayList<LatLng> decodePoly(String encoded) {
ArrayList<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
poly.add(position);
}
return poly;
}
}
activity_main.xml中
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_normal"
android:textSize="@dimen/textsize" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_satellite"
android:textSize="@dimen/textsize" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_terrain"
android:textSize="@dimen/textsize" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_hybrid"
android:textSize="@dimen/textsize" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/btn_locate"
android:textSize="@dimen/textsize" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_destination"
android:textSize="@dimen/textsize" />
</LinearLayout>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.Disthanitian.thaistyle.EmerMez"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"/>
<permission
android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<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="app.Disthanitian.thaistyle.EmerMez.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBzRmlT-LnuOeDHpxByJJVq8cxc39JTDGg"/>
</application>
</manifest>
logcat的
06-06 10:35:34.032: I/ADM(1710): 2: 825935789: [INFO] hw_handler_u8500:849 Query: SELECT * FROM HW_Settings WHERE (Platform = 'AB8500') AND ('REF_Speaker_Playback_Music' IN (Dev1, Dev2, Dev3, Dev4, Dev5, Dev6))ORDER BY rowid LIMIT 1
06-06 10:35:34.052: W/System.err(6196): android.os.NetworkOnMainThreadException
06-06 10:35:34.052: W/System.err(6196): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-06 10:35:34.052: W/System.err(6196): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-06 10:35:34.052: W/System.err(6196): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-06 10:35:34.062: W/System.err(6196): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-06 10:35:34.062: W/System.err(6196): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-06 10:35:34.072: W/System.err(6196): at app.Disthanitian.thaistyle.EmerMez.Direction.getDocument(Direction.java:41)
06-06 10:35:34.072: W/System.err(6196): at app.Disthanitian.thaistyle.EmerMez.MainActivity$6.onClick(MainActivity.java:175)
06-06 10:35:34.072: W/System.err(6196): at android.view.View.performClick(View.java:4162)
06-06 10:35:34.072: W/System.err(6196): at android.view.View$PerformClick.run(View.java:17082)
06-06 10:35:34.072: W/System.err(6196): at android.os.Handler.handleCallback(Handler.java:615)
06-06 10:35:34.072: W/System.err(6196): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 10:35:34.072: W/System.err(6196): at android.os.Looper.loop(Looper.java:137)
06-06 10:35:34.072: W/System.err(6196): at android.app.ActivityThread.main(ActivityThread.java:4867)
06-06 10:35:34.072: W/System.err(6196): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 10:35:34.082: W/System.err(6196): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 10:35:34.082: W/System.err(6196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-06 10:35:34.082: W/System.err(6196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-06 10:35:34.082: W/System.err(6196): at dalvik.system.NativeStart.main(Native Method)
06-06 10:35:34.082: D/AndroidRuntime(6196): Shutting down VM
06-06 10:35:34.082: W/dalvikvm(6196): threadid=1: thread exiting with uncaught exception (group=0x41c612a0)
06-06 10:35:34.092: E/AndroidRuntime(6196): FATAL EXCEPTION: main
06-06 10:35:34.092: E/AndroidRuntime(6196): java.lang.NullPointerException
06-06 10:35:34.092: E/AndroidRuntime(6196): at app.Disthanitian.thaistyle.EmerMez.Direction.getDurationValue(Direction.java:62)
06-06 10:35:34.092: E/AndroidRuntime(6196): at app.Disthanitian.thaistyle.EmerMez.MainActivity$6.onClick(MainActivity.java:176)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.view.View.performClick(View.java:4162)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.view.View$PerformClick.run(View.java:17082)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.os.Handler.handleCallback(Handler.java:615)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.os.Looper.loop(Looper.java:137)
06-06 10:35:34.092: E/AndroidRuntime(6196): at android.app.ActivityThread.main(ActivityThread.java:4867)
06-06 10:35:34.092: E/AndroidRuntime(6196): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 10:35:34.092: E/AndroidRuntime(6196): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 10:35:34.092: E/AndroidRuntime(6196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-06 10:35:34.092: E/AndroidRuntime(6196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-06 10:35:34.092: E/AndroidRuntime(6196): at dalvik.system.NativeStart.main(Native Method)
06-06 10:35:34.092: I/ADM(1710): 8: 825995695: [INFO] hw_handler_u8500:961 ste_adm_hw_handler_u8500: Found matching HW-settings: devices = ('REF_Speaker_Playback_Music', '', '', '', '', ''), d2dlogical-connections = ('', '', '', '').
06-06 10:35:34.162: I/ADM(1710): 7: 826061094: [INFO] dev:429 Device Speaker is OUTPUT
06-06 10:35:34.162: I/ADM(1710): 9: 826061186: [INFO] dev_omx:1276 Enter dev_omx_setup_graph - device=Speaker, samplerate=44100, format=2, alloc_buffers=0, is_vc=0, bufsz=0
06-06 10:35:34.162: E/android.os.Debug(2035): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
答案 0 :(得分:1)
您的Direction.java似乎是f9 ...但是您需要修改 MainActivity ,您尝试执行的代码点击按钮 ..运行它AsynTask(即创建一个必须扩展AsyncTask的私有类)并在你点击按钮时调用这个asyn-class的execute()...... !!!
这肯定会解决你的 06-06 10:35:34.052: W / System.err(6196):android.os.NetworkOnMainThreadException 错误。希望这个wud还解决了诸如“java.lang.NullPointerException”之后的异常。因为“doc” - 对象本身可能为null,因为代码抛出了NetworkException,进一步提供null响应&amp;因此,无论你有“doc” -obj依赖性,都会导致异常...... !!!
下面我附上了一个你可以参考的代码片段.. !! 如果您想详细跟进,可以参考"show route on google-maps v2“
private class showRoute extends AsyncTask<Void, Void, Document>{
Document doc;
PolylineOptions rectLine;
@Override
protected Document doInBackground(Void... params) {
doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
return null;
}
@Override
protected void onPostExecute(Document result) {
mMap.addPolyline(rectLine);
}
}
答案 1 :(得分:0)
您的代码中存在两个错误,它们与Polylines
无关。
android.os.NetworkOnMainThreadException
这意味着您正在尝试在主线程上运行阻止代码。您需要使用AsyncTask
,Thread
,执行者或其他不阻止用户界面的方法。
java.lang.NullPointerException
您Direction.getDurationValue
内的某个地方(正好在第62行),您正试图在null
引用上调用方法。