如何在Android中的Widget中创建Google Maps

时间:2012-02-22 11:24:44

标签: android google-maps webview widget

我正在考虑制作可以显示Google地图的Android小部件。 虽然我已经创建了两个类,一个继承了MapActivity,另一个继承了AppWidgetProvider,但我不知道如何使用它们。任何人都可以帮忙吗?

package com.leaning.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

public class MapWidgo extends AppWidgetProvider {

MapGoogle obj = new MapGoogle();

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);

for(int i=0;i<appWidgetIds.length;i++)
{
    int appWidgetId = appWidgetIds[i];

    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.main);
    remote.setOnClickPendingIntent(R.id.webView1, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, remote);

}
}   
}    

现在下面是我的MapGoogle类

package com.leaning.widget;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class MapGoogle extends MapActivity {
@Override
protected void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);

    MapView mapview = (MapView) findViewById(R.id.mySecretMap);
    mapview.setBuiltInZoomControls(true);

    final MapController controller = mapview.getController();

    LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener listener = new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            controller.setCenter(new GeoPoint((int)location.getLongitude(),(int)location.getLatitude()));
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, listener);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

,最后一个是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.leaning.widget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application

    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />

    <receiver android:name=".MapWidgo">

        <intent-filter>
           <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>

       <meta-data android:name="android.appwidget.provider"     android:resource="@xml/widgetresource"/>
   </receiver>

    <activity android:name=".MapGoogle">

    </activity>

</application>

我想要一个快速解决方案!

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

也许你可以拥有一个网页服务,它可以读取谷歌地图网址并将其转换为图片。 您所需要的只是一个能够显示来自网址的图片的小部件(Play上有很多图片)