每个像素的纬度和经度谷歌静态地图

时间:2015-08-01 17:17:27

标签: java android google-static-maps

我有静态地图,我知道纬度和经度的中心坐标,缩放级别和图像尺寸。 我不明白谷歌静态地图是否已经扁平化,每个像素的纬度和经度在任何地方都是相同的。如果它不是那样我想知道如何计算每个像素的latidude和经度,然后计算每像素的米数,以及这些值如何随纬度变化。 附:我正在开发一个下载静态地图并计算2点之间距离的Android应用程序,但我已经做到了这一点,因为lat和每个像素的长度是相同的...并且不依赖于纬度(地球上的位置) 这是我的主要活动

    package com.mnav.nav;

     import android.app.*;
     import android.os.*;
     import android.view.*;
     import android.widget.*;
     import android.view.View.*;
     import java.net.*;
     import org.apache.http.*;
     import android.graphics.*;
     import java.io.*;
     import android.graphics.drawable.*;

   public class MainActivity extends Activity
    {    Button start, dist;
    TextView txt;
     EditText lat, longit, zoom;
     ImageView img;
     Handler mhandler ;
     Bitmap bit, original;
     Boolean run = false, firstpoint = false, secondpoint = false;
     String z, latitude, longitude;
 int level;
 int[] pointa = new int[2];
 int[] pointb = new int[2];
 double unit;
 int maxx, maxy, minx, miny, distx, disty,  distance1;
 double distance;


             /** Called when the activity is first created. */
            @Override
              public void onCreate(Bundle savedInstanceState)
         {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    start=(Button)findViewById(R.id.button);
    lat = (EditText)findViewById(R.id.latitude);
    longit = (EditText)findViewById(R.id.longitude);
    img = (ImageView)findViewById(R.id.img);
    zoom =(EditText)findViewById(R.id.zoom);
    dist = (Button)findViewById(R.id.dist);
    txt = (TextView)findViewById(R.id.txt);
    mhandler = new Handler();
    start.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {   z = zoom.getText().toString();
                latitude = lat.getText().toString();
                longitude = longit.getText().toString();
                Thread thr = new Thread(new GetImage());
                thr.start();


            }


    });

    img.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {  int i, j;

                int x = (int)event.getX();
                int vertical = (int)event.getY();
                if(secondpoint==true)
                {   bit = original.copy(Bitmap.Config.ARGB_4444, true);
                    img.setImageBitmap(bit);
                    secondpoint = false;
                }

                txt.setText(Integer.toString(x)+ " "+Integer.toString(vertical));
                for (i = x-2; i < x+3; i++)
                {
                    for(j=vertical-2; j<vertical+3; j++)
                    {
                        bit.setPixel(i,j, Color.WHITE);
                    }
                }
                bit.setPixel(x,vertical,Color.GREEN);

                img.setImageBitmap(bit);
                if(firstpoint)
                {
                    pointb[0] = x; pointb[1]=vertical;
                    secondpoint = true;
                    firstpoint = false;
                }
                else 
                {
                    pointa[0]=x; pointa[1]=vertical;
                    firstpoint=true;
                }

                return false;
            }


    });
    dist.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View p1)
           {   



                unit = 0.1412/800;
                unit = unit*500;
                level = Integer.parseInt(z);
                if(level == 20)
                {
                    unit = unit;
                }
                else 
                    if(level == 19)
                    {
                        unit = unit*2;
                    }
                    else
                    if(level==18)
                    {
                        unit = unit*4;
                    }
                    else 
                    if(level == 17)
                    {
                        unit =unit*8;
                    }
                    else
                    if(level==16)
                    {
                        unit = unit*16;
                    }
                    else
                    if(level==15)
                    {
                        unit = unit*32;
                    }
                    else
                    if(level==14)
                    {
                        unit = unit*64;
                    }
                    else 
                    if(level==13)
                    {
                        unit = unit*128;
                    }
                    else 
                    if(level == 12)
                    {
                        unit = unit*256;
                    }
                    else 
                    if(level==11)
                    {
                        unit = unit*512;
                    }
                    else
                    if(level==10)
                    {
                        unit = unit*1024;
                    }




                     distx = Math.abs(pointa[0]-pointb[0]);
                     disty= Math.abs(pointa[1]-pointb[1]);

                    distance1 = distx*distx+disty*disty;
                    distance = Math.sqrt(distance1);


                    distance = unit*distance;
                    txt.setText(Double.toString(distance)+" meters");


            }


    });



}
public class GetImage implements Runnable
{
    String Url = "http://maps.google.com/staticmap?center=" + latitude+","+longitude+"&format=png&zoom="+ z +"&size=500x500&scale=1&maptype=hybrid&key=ABQIAAAA-O3c-Om9OcvXMOJXreXHAxRexG7zW5nSjltmIc1ZE-b8yotBWhQYQEU3J87QIBc4nfuySpoW_K6woA"; 
    Bitmap bmp;

    @Override
    public void run()
    { 

        try
        {   mhandler.post(new Runnable()
                {

                    @Override
                    public void run()
                    {

                        txt.setText("Wait...file is downloading...");
                    }


            });
            bmp = BitmapFactory.decodeStream((InputStream)new URL(Url).getContent());


        }
        catch (IOException e)
        {   mhandler.post(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        // TODO: Implement this method




            Toast.makeText(getApplicationContext(), "Something wrong just happend", Toast.LENGTH_LONG).show();

                    }
            });
            e.printStackTrace();
        }


        mhandler.post(new Runnable()
            {

                @Override
                public void run()
                { 

                if (bmp!=null)
                 {   txt.setText("Map downloaded..");
                    img.setImageBitmap(bmp);
                    bit = ((BitmapDrawable)img.getDrawable()).getBitmap();
                    bit = bit.copy(Bitmap.Config.ARGB_4444, true);
                    original = bit.copy(Bitmap.Config.ARGB_4444, true);
                 }
                 else 
                 {
                     mhandler.post(new Runnable()
                         {

                             @Override
                             public void run()
                             {
                                 Toast.makeText(getApplicationContext(), "null bitmap, sorry", Toast.LENGTH_LONG).show();
                             }


                     });
                 }
                }


        });
    }


}

}

0 个答案:

没有答案