如何在 Android 的 Widget 中创建 Google 地图

发布于 2025-01-08 04:12:07 字数 4100 浏览 1 评论 0原文

我正在考虑为 Android 制作可以显示 Google 地图的小部件。 虽然我创建了两个类,一个继承 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>

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

I am thinking of making widget for Android that can display Google Maps.
Though i have made two classes one inheriting with MapActivity and the other with AppWidgetProvider but i am not sure how to use them. Could any one help it out?

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);

}
}   
}    

now below is my MapGoogle Class

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;
}
}

and the last one is my manifest file

<?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>

I want a quick solution!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2025-01-15 04:12:07

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

maybe you can have a web service which reads a google map url and convert it to an image.
The,n what you need is simply a widget able to show an image from a web URL (there are many on Play)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文