Android应用里面调用Google Earth应用

说明:这里的调用是指调用Google Earth应用程序来标识某个地点,而不是像Google Map一样有可调用的API,可以嵌于到自己的应用之中。

该测试程序是在调研Google Earth API的过程中看到并动手测试了,Google Earth并没有开放的API。

测试该程序前,需安装Google Earth 应用(Android版),当然了,如果没有安装,调用时会提示你安装的。

程序就一个按钮,点击后会调用Google Earth 并定位到“中国北京”。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Google Eearth" />
</LinearLayout>

主文件:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TutorialPartI extends Activity {
/** Called when the activity is first created. */
private Button btn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
//设置应用程序全屏显示
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
setContentView(R.layout.main);
 
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(btnListener);
 
}
 
//按钮事件
private OnClickListener btnListener = new OnClickListener() {
 
public void onClick(View view) {
 
// the new intent we will launch
Intent myIntent = new Intent();
 
myIntent.setClassName("com.google.earth", "com.google.earth.EarthActivity");
 
// we are doing a search query
myIntent.setAction(Intent.ACTION_SEARCH);
 
// change this address to any address you want to fly to
myIntent.putExtra(SearchManager.QUERY, "beijing china");
 
try {
// launch google earth and fly to location
//TutorialPartI.startActivity(myIntent);
startActivityForResult(myIntent, 0);
}
catch (ActivityNotFoundException e) {
showGoogleEarthDialog();
}
}
};
 
// 如果没有安装Google Earth应用,则提示用户安装
private void showGoogleEarthDialog() {
 
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);
downloadDialog.setTitle("Install Google Earth?");
downloadDialog.setMessage("This application requires Google Earth. Would you like to install it?");
downloadDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:com.google.earth");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
TutorialPartI.this.startActivity(intent);
}
});
downloadDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {}
});
downloadDialog.show();
}
}
原文地址:https://www.cnblogs.com/wzc0066/p/2949200.html