接近警报不起作用,我用 google maps api v2 制作了一个简单的 android 应用程序

Posted

技术标签:

【中文标题】接近警报不起作用,我用 google maps api v2 制作了一个简单的 android 应用程序【英文标题】:Proximity Alerts not working,I've made a simple android app with google maps api v2 【发布时间】:2015-03-29 12:09:54 【问题描述】:

Proximity Alert 根本没有出现。抱歉,代码太长了。我已经为此工作了一天多,现在仍然找不到解决方案。任何帮助将不胜感激。

附:我是编程和 android 新手,我正在研究位置警报(基于接近的警报)

这是调用接近警报的函数

       private void dispdist(LatLng dest)
       
      float[] distance=new float[3];
      try 
        double lat2 = dest.latitude;
        double lon2 = dest.longitude;
        double lat1 = mMap.getMyLocation().getLatitude();
        double lon1 = mMap.getMyLocation().getLongitude();
        //t1.setText(dest.toString());
         Location destination;
        destination=new Location();
        destination.setLatitude(dest.latitude);
        destination.setLongitude(dest.longitude);
        location.distanceBetween(lat1, lon1, lat2, lon2, distance);
        Toast.makeText(getApplicationContext(), "Distance: " 
       + distance[0], Toast.LENGTH_SHORT).show();
        drawCircle(dest);
        addProximityAlerts(destination.getLatitude()
        , destination.getLongitude());
        catch (Exception e) 
        Log.d("Android: ", "Marker drag end");
       
       

所以这是我触发接近警报的功能

    private void addProximityAlerts(double latitude, double longitude)
  
    LocationManager locationManager = (LocationManager) 
        getSystemService(LOCATION_SERVICE);
             if(prox==false)
                 
                     Intent intent = new Intent(PROX_ALERT_INTENT);
                    // Toast.makeText(getApplicationContext(),"Into add 
                //  proximity",Toast.LENGTH_SHORT)
                 // .show();
           PendingIntent proximityIntent = 
                      PendingIntent.getBroadcast(this, 0, intent, 0);
                    // Toast.makeText(getApplicationContext(),
                    //"Addedproximity",Toast.LENGTH_SHORT).show();
                    locationManager.addProximityAlert(latitude,
                    longitude, 50, 60000, proximityIntent);

                       IntentFilter filter = new 
                              IntentFilter(PROX_ALERT_INTENT);
                    // Toast.makeText(getApplicationContext(),
                    //  "added proximity1",Toast.LENGTH_SHORT).show();
               registerReceiver(new ProximityIntentReceiver(), filter);

              prox=true;

             
              else
             
                 Intent anIntent=new Intent(PROX_ALERT_INTENT);
                 PendingIntent operation =
                         PendingIntent
                .getBroadcast(getApplicationContext()
                  , 0 , anIntent, 0);
                 locationManager.removeProximityAlert(operation);
                // Toast.makeText(getApplicationContext(),
               //"Removed last proximity alert",Toast.LENGTH_SHORT)
                //.show();
                 prox=false;
                 addProximityAlerts(latitude,longitude);

                  

清单

   <uses-permission android:name="android.permission.INTERNET" />
<uses-permission 
            android:name="android.permission.ACCESS_NETWORK_STATE"       
    />
<uses-permission  android:name=
                     "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=
       "com.google.android.providers.gsf.permission.READ_GSERVICES" />
 <uses-permission android:name=
       "com.example.androidmapv2ex.permission.MAPS_RECEIVE" />
  <!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but are recommended.
   -->
      <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
     <uses-permission 
      android:name="android.permission.ACCESS_FINE_LOCATION" 
       />
      <uses-permission android:name="android.permission.VIBRATE" />
     <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <receiver
            android:name=".ProximityIntentReceiver">
        </receiver>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>

     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    </application>

       </manifest>

ProximityIntentReciever

    public class ProximityIntentReceiver extends BroadcastReceiver 

     private static final int NOTIFICATION_ID = 1000;

    @Override
     public void onReceive(Context context, Intent intent) 
    Toast.makeText(context,"Into Recieve",Toast.LENGTH_SHORT).show();

    String key = LocationManager.KEY_PROXIMITY_ENTERING;

    Boolean entering = intent.getBooleanExtra(key, false);

    if (entering) 
        Log.d(getClass().getSimpleName(), "entering");
    
    else 
        Log.d(getClass().getSimpleName(), "exiting");
    
    Toast.makeText(context,"Into Recieve1",Toast.LENGTH_SHORT).show();
    NotificationManager notificationManager =
            (NotificationManager)   

        context.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
     null, 0);

    Notification notification = createNotification();
    notification.setLatestEventInfo(context,
            "Proximity Alert!", "Entered",pendingIntent);

     notificationManager.notify(NOTIFICATION_ID, notification);

    

   private Notification createNotification() 
    Notification notification = new Notification();

    notification.icon = R.drawable.dest_ic;
    notification.when = System.currentTimeMillis();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    notification.ledARGB = Color.WHITE;
    notification.ledOnMS = 1500;
    notification.ledOffMS = 1500;

    return notification;
        

          

【问题讨论】:

有人愿意回复吗? 【参考方案1】:

将您的注册接收器更改为此将解决您的问题。

this.registerReceiver(new ProximityIntentReceiver(), filter)

【讨论】:

以上是关于接近警报不起作用,我用 google maps api v2 制作了一个简单的 android 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

为啥“Google Maps”一书中的 Google Maps Directions API 示例对我不起作用?

Angular Google Maps 组件图标不起作用

Google Map API setCenter 方法不起作用

使用 phonegap 构建服务的插件 plugin.google.maps 在 ios 上不起作用

Google Maps API imagePath 本地图像不起作用

Angular Google Maps 组件更改选项不起作用