在地图上绘制行车方向

时间:2013-12-19 07:02:31

标签: android google-maps

我是android地图开发的新手。我试图通过urlget Document type object。它产生了错误。
发生了跟随错误。

这是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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout> 

这是主要的活动代码。

  public class MainActivity extends Activity  {
          static final LatLng Venushka = new LatLng(6.915861, 79.865368);
          static final LatLng venushka2 = new LatLng(6.922763, 79.862053);
          public GoogleMap map;

          String test;
          GMapV2Direction path;
          LatLng start=new LatLng(6.915883,79.8654);
          LatLng end=new LatLng(6.922784,79.862074);
              @Override
          protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
            Marker hamburg = map.addMarker(new MarkerOptions().position(Venushka) .title("Venushka")
                    .snippet("Venushka is cool")
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_ven)));

            Marker kiel = map.addMarker(new MarkerOptions()
                .position(venushka2)
                .title("Venushka 2")
                .snippet("Venushka is happy")
                .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_ven)));



            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(Venushka, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

            if(map != null)
            {
                map.setMyLocationEnabled(true);
                map.getUiSettings().setCompassEnabled(true);

            } 


            path=new GMapV2Direction();

            Document doc=path.getDocument(start,end, "");

            String value=doc.toString();

            //Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG).show();
            Log.d(">>>>>>>>",value);

          }
        }

这是我的GMapV2Direction类代码

public class GMapV2Direction {
    public final static String MODE_DRIVING = "driving";
    public final static String MODE_WALKING = "walking";
    Document doc=null;

    public GMapV2Direction() { }

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

   }

这是清单代码。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testmaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <permission
        android:name="com.example.testmaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.testmaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testmaps.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="AIzaSyDV5vEA9FdJ4cyINLegzPTGW0twdB83p38" />
    </application>

</manifest>

这是我的LogCat

12-19 12:29:34.498: E/AndroidRuntime(16435): FATAL EXCEPTION: main
12-19 12:29:34.498: E/AndroidRuntime(16435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testmaps/com.example.testmaps.MainActivity}: java.lang.NullPointerException
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.os.Looper.loop(Looper.java:137)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread.main(ActivityThread.java:4511)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at java.lang.reflect.Method.invokeNative(Native Method)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at java.lang.reflect.Method.invoke(Method.java:511)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at dalvik.system.NativeStart.main(Native Method)
12-19 12:29:34.498: E/AndroidRuntime(16435): Caused by: java.lang.NullPointerException
12-19 12:29:34.498: E/AndroidRuntime(16435):    at com.example.testmaps.MainActivity.onCreate(MainActivity.java:101)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.Activity.performCreate(Activity.java:4470)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-19 12:29:34.498: E/AndroidRuntime(16435):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
12-19 12:29:34.498: E/AndroidRuntime(16435):    ... 11 more
12-19 12:29:37.633: D/dalvikvm(16435): GC_CONCURRENT freed 144K, 4% free 13897K/14343K, paused 3ms+2ms

1 个答案:

答案 0 :(得分:0)

为什么不参加我在这个主题上写的博客文章:

Google Maps V2 for Android: Draw Driving Direction on Map

在这篇文章的末尾(Section 6),你会发现一个提供这个功能的工作项目,你所要做的就是下载它,在工作环境中导入,生成你自己的Google Maps API V2 key使用API​​控制台并将其应用于项目。然后运行项目,它应该工作。

相关问题