Wednesday 22 February 2012

Displaying map in web view using google service in android


we can display the maps in web view using this service

http://maps.google.com/maps?q=address,city,state,zipcode

Sample code for using this service is

setContentView(R.layout.webview);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setVisibility(pb.VISIBLE);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
pb.setProgress(progress);
if (progress == 100) {
pb.setVisibility(pb.GONE);
}
}
});

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
String addressText = address + "," + city + "," + state + ","+ zip;
addressText = addressText.replaceAll(" ", "%20");
addressText = addressText.replaceAll("#", "%23");
mWebView.loadUrl("http://maps.google.com/maps?q="+ addressText);

----
public class HelloWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);

return true;
}
}

and we need to provide some background for the progressbar
use the progress bar code in our display xml

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" android:soundEffectsEnabled="true" android:indeterminateDrawable="@drawable/progress_bar_states"/>

use the progress bar background by using xml like progress_bar_states.xml. wee
need to place this xml file in drawable folder

code for progress_bar_states.xml is

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">

    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">

        <size android:width="48dip" android:height="48dip" />

        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#4c737373" android:centerColor="#00FF00"
            android:centerY="0.50" android:endColor="#ffffd300" />

    </shape>

</rotate>


No comments:

Post a Comment