【问题标题】:How to write a program using google map javascript apiv3如何使用谷歌地图javascript apiv3编写程序
【发布时间】:2011-02-01 05:38:00
【问题描述】:

如何使用 Google Maps Javascript API V3 Services 编写一个 android 程序,我正在使用 eclipse...

http://code.google.com/apis/maps/documentation/javascript/services.html#Directions

从上面我给出的链接中,我想尝试方向示例,但他们在那里使用了一些 html 代码,但是我必须在我的 eclipse 的新项目中编写 html 脚本代码,然后我应该如何链接 html 和安卓代码..

我还看到有人说我们需要在代码中包含http://maps.google.com/maps/api/js?sensor=false 脚本..如何包含这个..?

请给我建议,提前谢谢..

【问题讨论】:

    标签: javascript android html


    【解决方案1】:

    将网页加载到 Android 应用程序是使用 WebView 完成的。因此,您可以使用它在原生 Android 应用程序中加载 Maps API 站点。 How To.

    【讨论】:

    • 嗨,谢谢,我已经浏览了那个网站,他们使用了一些 java 脚本来获取 web 功能,但是我应该在我的 eclipse java 项目中在哪里编写 java 脚本..
    • 你必须在你的应用程序代码和你的javascript代码之间创建接口来绑定它们,这里你去developer.android.com/guide/webapps/…
    【解决方案2】:

    试试下面的代码。它对我来说很好。

    //First add permission in manifest file:
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    
    
    public class webViewes extends Activity {
    private static String PROVIDER="gps";
    private WebView browser;
    private LocationManager myLocationManager=null;
    
    @Override
    public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      browser=(WebView)findViewById(R.id.webview);
      myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
      browser.getSettings().setJavaScriptEnabled(true);
      browser.addJavascriptInterface(new Locater(), "locater");     
      browser.loadUrl("http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html");
    }
    
    @Override
    public void onResume() {
      super.onResume();
      myLocationManager.requestLocationUpdates(PROVIDER, 0, 0, onLocation);
    }
    
    @Override
    public void onPause() {
      super.onPause();
      myLocationManager.removeUpdates(onLocation);
    }
    
    LocationListener onLocation=new LocationListener() {
      public void onLocationChanged(Location location) {
        StringBuilder buf=new StringBuilder("javascript:whereami(");
    
        buf.append(String.valueOf(location.getLatitude()));
        buf.append(",");
        buf.append(String.valueOf(location.getLongitude()));
        buf.append(")");
    
        browser.loadUrl(buf.toString());
      }
    
      public void onProviderDisabled(String provider) {
      }
      public void onProviderEnabled(String provider) {
      }
      public void onStatusChanged(String provider, int status,Bundle extras) {
      }
    };
    
    public class Locater {
      public String getLocation() throws JSONException {
        Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
    
        if (loc==null) {
          return(null);
        }
    
        JSONObject json=new JSONObject();
    
        json.put("lat", loc.getLatitude());
        json.put("lon", loc.getLongitude());
    
        return(json.toString());
      }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2023-03-21
      • 2017-11-26
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多