http://www.google.cn Android 生成谷歌地图

<div class="iteye-blog-content-contain" style="font-size: 14px;">
<p>MainActivity</p>
<pre name="code" class="java">package com.example.test1;

import java.util.Locale;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private static final String MAP_URL = "http://ditu.google.cn/maps?hl=zh&amp;mrt=loc&amp;q=31.1198723,121.1099877&amp;q=(上海青浦大街100号)";

private static final String MAP_URL1 = "https://www.google.com/maps/dir/@41.4943358,-73.1530549,8z/data=!4m13!4m12!1m5!1m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!2m2!1d-74.0059413!2d40.7127837!1m5!1m1!1s0x89e3652d0d3d311b:0x787cbf240162e8a0!2m2!1d-71.060097!2d42.3584865";
  
private int gMapZoom = 8;
private WebView gWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// webView = (WebView) findViewById(R.id.webView1);
//
// webView.getSettings().setJavaScriptEnabled(true);//设置使用够执行JS脚本
// webView.getSettings().setBuiltInZoomControls(true);//设置使支持缩放
// webView.loadUrl("http://ditu.google.cn/maps?hl=zh&amp;mrt=loc&amp;q=-74,40.7167");

        setupWebView();
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

  private void setupWebView(){
  
        final String centerURL = "javascript:initialize();"
                               + "centerAt(" + -74 + "," + 40.7167 + ");"
                               + "map.setZoom(" + gMapZoom + ");";
       
        gWebView = (WebView)findViewById(R.id.webview);
       
        // JavaScript 啟用
        gWebView.getSettings().setJavaScriptEnabled(true);
       
        // 網頁執行完成後, 接著要執行的網頁 (也可是 JavaScript)
        gWebView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String url){
                view.loadUrl(centerURL);
            }
        });
       
        gWebView.loadUrl(MAP_URL);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void gotoMap(View view){
double  sourceLatitude = -74;
double  sourceLongitude = 40.7167;
double  destinationLatitude = 38.53;
double  destinationLongitude = 70.02;

String directionweburl = "http://maps.google.com/maps?daddr="+sourceLatitude+","+sourceLongitude+"&amp;saddr="+destinationLatitude+","+destinationLongitude;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(directionweburl));
startActivity(myIntent);

// String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&amp;daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", destinationLatitude, destinationLongitude, "Where the party is at");
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
// intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
// startActivity(intent);


}


}


 

 mian.xml

<?xml version="1.0"  encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="gotoMap"
        android:text="@string/app_name" >
    </Button>

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

猜你喜欢

转载自dasheny.iteye.com/blog/2171594