我正在构建一个Android应用程序,它将整合谷歌地图api 3中的地图。我已经阅读了这个tutorial并且无法理解这段代码的作用。
private void setupWebView(){
final String centerURL = "javascript:centerAt(" +
mostRecentLocation.getLatitude() + "," +
mostRecentLocation.getLongitude()+ ")";
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url){
webView.loadUrl(centerURL);
}
});
webView.loadUrl(MAP_URL);
}
有人可以彻底向我解释这些代码的作用。谢谢!
答案 0 :(得分:2)
迟到了,但也许它会帮助别人。
我不太确定,因为我是初学者,但我的猜测是你正在调用名为“centerAt”的JavaScript函数,看一下这个教程https://developers.google.com/maps/articles/android_v3,你会看到用这个教程的JavaScript桥定义功能“centerAt”
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map;
function initialize() {
var latitude = 0;
var longitude = 0;
if (window.android){
latitude = window.android.getLatitude();
longitude = window.android.getLongitude();
}
var myLatlng = new google.maps.LatLng(latitude,longitude);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
function centerAt(latitude, longitude){
myLatlng = new google.maps.LatLng(latitude,longitude);
map.panTo(myLatlng);
}
</script>
总而言之,您真正在做的是使用JavaScript桥在Android应用中显示地图,因此您可以使用Android代码中定义的JavaScript函数,通过URL调用它们。
同样,我是初学者(谷歌地图的第一天),可能我错了。