android 高德地图Demo 的使用
2、高德地图 记得要加入Key值,name不用修改;将主Avtivity变一下,剩下的删了!
3、修改 Assistant_Location_Activity:
package com.amap.location.demo;
import com.amap.api.location.AMapLocationClient;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
/**
* H5辅助定位模式功能演示
*
* @创建时间: 2015年11月24日 下午4:24:07
* @项目名称: AMapLocationDemo2.x
*
* @author hongming.wang
* @文件名称: Battery_Saving_Activity.java
* @类型名称: Battery_Saving_Activity
*/
public class Assistant_Location_Activity extends Activity implements OnClickListener {
private AMapLocationClient locationClient = null;
private WebView webView;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//不顯示标题
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assistant_location);
webView = (WebView) findViewById(R.id.webView);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stubs
Intent intent=new Intent();
//包名 包名+类名(全路径)
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);
finish();//停止当前的Activity,如果不写,则按返回键会跳转回原来的Activity
}
});
locationClient = new AMapLocationClient(getApplicationContext());
locationClient.startAssistantLocation();
webView.setVisibility(View.VISIBLE);
webView.loadUrl(Utils.URL_H5LOCATION);
initWebView();
}
private void initWebView(){
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// 允许webview执行javaScript脚本
webSettings.setJavaScriptEnabled(true);
// 设置是否允许定位,这里为了使用H5辅助定位,设置为false。
//设置为true不一定会进行H5辅助定位,设置为true时只有H5定位失败后才会进行辅助定位
webSettings.setGeolocationEnabled(false);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.setWebChromeClient(new WebChromeClient() {
// 处理javascript中的alert
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
return true;
};
// 处理javascript中的confirm
public boolean onJsConfirm(WebView view, String url,
String message, final JsResult result) {
return true;
};
// 处理定位权限请求
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback) {
callback.invoke(origin, true, false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
@Override
// 设置网页加载的进度条
public void onProgressChanged(WebView view, int newProgress) {
Assistant_Location_Activity.this.getWindow().setFeatureInt(
Window.FEATURE_PROGRESS, newProgress * 100);
super.onProgressChanged(view, newProgress);
}
// 设置应用程序的标题title
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != locationClient) {
locationClient.stopAssistantLocation();
locationClient.onDestroy();
}
if(null != webView){
webView.destroy();
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
4、修改布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webView"
android:visibility="gone"
android:layout_marginTop="@dimen/normal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="56dp"
android:layout_marginRight="58dp"
android:text="Button" />
</RelativeLayout>