0

对不起我的英语。

我正在开发关于区域检测的应用程序。我想通过 GPS 检测 android 设备的位置,当它在折线上时,它会提醒用户。所以我想将折线保留在本地存储中。

但是不知道怎么保存呢?

我的解决方案是:

  • 我将起点和终点(纬度,经度)保存在数据库中,然后将参数发送到 maps.google.com 并访问节点,然后编写文档并从文档创建折线

错误 - 单击“风险区”后无法运行它在地图上仅显示标记,但不显示折线。它会崩溃。

主要活动

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);


            }
        });




    }
}   

方向.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>

日志猫

      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
4

2 回答 2

1

您的 Direction.java 似乎是 f9 ...但是您需要修改MainActivity,您尝试在按钮单击时执行的代码..通过 AsynTask 运行它(即创建一个必须扩展 AsyncTask 的私有类)并调用执行() 这个异步类在你的按钮上单击...!!!

这肯定会解决您的 06-06 10:35:34.052: W/System.err(6196): android.os.NetworkOnMainThreadException 错误。希望这个 wud 也解决了诸如“java.lang.NullPointerException”之类的异常......因为“doc” - 对象本身可能为空,因为代码抛出了一个 NetworkException,进一步给出一个空响应,因此无论你在哪里都会导致异常有一个“doc” -obj 依赖...!!!

下面我附上了一个代码片段,你可以参考它..!! 如果您想详细跟进,可以参考此“在 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);
            }   

        }
于 2013-10-04T17:12:37.097 回答
0

There are two errors in your code and they are not related to Polylines.

android.os.NetworkOnMainThreadException

This means you are trying to run blocking code on main thread. You need to use AsyncTask, Thread, executors or other means of not blocking user interface.

java.lang.NullPointerException

Somewhere inside your Direction.getDurationValue (exactly on line 62) you are trying to call a method on null reference.

于 2013-06-06T06:56:50.467 回答