在Android中显示包含路线的Google静态地图

时间:2014-04-09 07:10:00

标签: android

我可以在谷歌静态地图上显示标记,但我想使用谷歌静态地图与两点之间的路线可能任何人告诉我如何在谷歌静态地图上显示路线黑白点 在这段代码中,第一个网址运行正常,但是当我使用路径网址它不工作时它给了我网址错误请任何人帮助我在静态网址中的路径 这是我的代码:

package com.example.staticmap;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView image;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image= (ImageView)findViewById(R.id.image);
   new abc().execute();
 //  image.setImageBitmap(bmp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    class abc extends AsyncTask<Void, Void, Bitmap>{
        double lati = 29.39444;
        double longi= 76.966041;

        @Override
        protected Bitmap doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Bitmap bmp = null;
        try{
String URL = "http://maps.google.com/maps/api/staticmap?center=&markers=color:blue%7Clabel:S%7C" +lati + "," + longi + "&zoom=15&size=200x200&maptype=roadmap&sensor=false";    

       //String URL = "http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=40.737102,-73.990318|40.749825,-73.987963|40.752946,-73.987384|40.755823,-73.986397&sensor=false";

       // bmp = null;
                HttpClient httpclient = new DefaultHttpClient();   
                HttpGet request = new HttpGet(URL); 

                InputStream in = null;
                try {
                    in = httpclient.execute(request).getEntity().getContent();
                    bmp = BitmapFactory.decodeStream(in);

                    in.close();

                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }catch(Exception ee){
            ee.printStackTrace();
        }

            return bmp;
        }

    @Override
        protected void onPostExecute(Bitmap result) {
            image.setImageBitmap(result);
        }

    }   
}

2 个答案:

答案 0 :(得分:0)

以下代码可帮助您在内置MAP中显示路线:

String fullAddress = "Your full address";

//Using address
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&daddr=" + fullAddress));

//Using Latitude,Longitude
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&daddr=51.448,-0.972"));

intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));

startActivity(intent);

答案 1 :(得分:-1)

您可以在图像视图中使用两个地理点之间路线的地图截图。这是在两个地理点之间绘制路线的一个很好的示例 http://wptrafficanalyzer.in/blog/drawing-driving-route-directions-between-two-locations-using-google-directions-in-google-map-android-api-v2/