Android下实现Google街景
Google街景确实是一个比较有趣的东西,准确来说它不是个东西,它是一个服务。是Google公司提供的一个服务。在现在很多手机上都加入了Google街景服务,起初没有去玩,后来接触了一下,感觉蛮不错的,能看到自己没去过的一些城市、国家的地方。
那如何在Android平台下实现Google街景服务呢,有以下几个步骤:
1.创建一个Google API 下的模拟器,这个很重要(在其他版本的模拟器下运行会失败)。
2.在程序中调用Google街景。
3.发挥你的想象力。
项目运行效果图:
神奇的东西来了,我们可以查看自己定位的地区的街景,目前我知道好像只有美国的部分地区才有相应的街景。
可以看到项目运行结果有一个小人,我们可以控制这个小人进行移动。可以像个小偷一样随意观看,当然看到的东西都是静态的。
下面是这个项目的源代码:
main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:text="@string/tvLong"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- />
- <EditText
- android:id="@+id/etLong"
- android:singleLine="true"
- android:text="-122.423743"
- android:layout_width="100px"
- android:layout_height="45px"/>
- <TextView
- android:text="@string/tvLat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:paddingLeft="8px"/>
- <EditText
- android:id="@+id/etLat"
- android:singleLine="true"
- android:text="37.788487"
- android:layout_width="100px"
- android:layout_height="45px"/>
- </LinearLayout>
- <Button
- android:id="@+id/btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn"/>
- </LinearLayout>
GoogleStreetView.java
- public class GoogleStreetView extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.btn); //获得Button对象
- btn.setOnClickListener(new View.OnClickListener() { //为按钮添加监听器
- @Override
- public void onClick(View v) { //重写onClick方法
- EditText etLong = (EditText)findViewById(R.id.etLong); //获取EditText控件
- EditText etLat = (EditText)findViewById(R.id.etLat); //获取EditText控件
- String sLong = etLong.getEditableText().toString().trim(); //获取输入的经度
- String sLat = etLat.getEditableText().toString().trim(); //获取输入的纬度
- if(sLong.equals("") || sLat.equals("")){ //如果没有输入经度或纬度
- Toast.makeText(GoogleStreetView.this,
- "请输入正确的经纬度!",
- Toast.LENGTH_LONG).show(); //输出错误信息
- return; //返回
- }
- String sUrl = "google.streetview:cbll="+sLat+","+sLong; //生成Uri字符串
- Intent i = new Intent(); //创建Intent对象
- i.setAction(Intent.ACTION_VIEW); //设置Intent的Action
- Uri uri = Uri.parse(sUrl); //生成Uri对象
- i.setData(uri); //设置Intent的Data
- startActivity(i); //发出Intent启动街景服务程序
- }
- });
- }
- }