4、Android下实现Google街景

来源:互联网 发布:冒用别人品牌淘宝开店 编辑:程序博客网 时间:2024/04/28 23:09

Android下实现Google街景

 

Google街景确实是一个比较有趣的东西,准确来说它不是个东西,它是一个服务。是Google公司提供的一个服务。在现在很多手机上都加入了Google街景服务,起初没有去玩,后来接触了一下,感觉蛮不错的,能看到自己没去过的一些城市、国家的地方。

 

那如何在Android平台下实现Google街景服务呢,有以下几个步骤:

1.创建一个Google API 下的模拟器,这个很重要(在其他版本的模拟器下运行会失败)。

2.在程序中调用Google街景。

3.发挥你的想象力。

 

 

项目运行效果图:

                                

 

 

 

 

 神奇的东西来了,我们可以查看自己定位的地区的街景,目前我知道好像只有美国的部分地区才有相应的街景。

可以看到项目运行结果有一个小人,我们可以控制这个小人进行移动。可以像个小偷一样随意观看,当然看到的东西都是静态的。

下面是这个项目的源代码:

main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.     <LinearLayout   
  8.         android:orientation="horizontal"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         >  
  12.         <TextView  
  13.             android:text="@string/tvLong"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_gravity="center_vertical"  
  17.             />  
  18.         <EditText   
  19.             android:id="@+id/etLong"  
  20.             android:singleLine="true"  
  21.             android:text="-122.423743"  
  22.             android:layout_width="100px"  
  23.             android:layout_height="45px"/>  
  24.         <TextView   
  25.             android:text="@string/tvLat"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_gravity="center_vertical"  
  29.             android:paddingLeft="8px"/>  
  30.         <EditText   
  31.             android:id="@+id/etLat"  
  32.             android:singleLine="true"  
  33.             android:text="37.788487"  
  34.             android:layout_width="100px"  
  35.             android:layout_height="45px"/>  
  36.     </LinearLayout>  
  37.     <Button   
  38.         android:id="@+id/btn"  
  39.         android:layout_width="match_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="@string/btn"/>  
  42. </LinearLayout>  


GoogleStreetView.java

[java] view plaincopy
  1. public class GoogleStreetView extends Activity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         Button btn = (Button)findViewById(R.id.btn);    //获得Button对象  
  8.         btn.setOnClickListener(new View.OnClickListener() { //为按钮添加监听器  
  9.             @Override  
  10.             public void onClick(View v) {               //重写onClick方法  
  11.                 EditText etLong = (EditText)findViewById(R.id.etLong);  //获取EditText控件  
  12.                 EditText etLat = (EditText)findViewById(R.id.etLat);    //获取EditText控件  
  13.                 String sLong = etLong.getEditableText().toString().trim();  //获取输入的经度  
  14.                 String sLat = etLat.getEditableText().toString().trim();    //获取输入的纬度  
  15.                 if(sLong.equals("") || sLat.equals("")){        //如果没有输入经度或纬度  
  16.                     Toast.makeText(GoogleStreetView.this,   
  17.                                     "请输入正确的经纬度!",   
  18.                                     Toast.LENGTH_LONG).show();  //输出错误信息  
  19.                     return;                                     //返回  
  20.                 }  
  21.                 String sUrl = "google.streetview:cbll="+sLat+","+sLong; //生成Uri字符串  
  22.                 Intent i = new Intent();                            //创建Intent对象  
  23.                 i.setAction(Intent.ACTION_VIEW);                //设置Intent的Action  
  24.                 Uri uri = Uri.parse(sUrl);                      //生成Uri对象  
  25.                 i.setData(uri);                                 //设置Intent的Data  
  26.                 startActivity(i);                               //发出Intent启动街景服务程序  
  27.             }  
  28.         });  
  29.     }  
  30. }  
0 0