【问题标题】:Working with radius in Google Map Android API 2在 Google Map Android API 2 中使用半径
【发布时间】:2015-02-16 21:54:34
【问题描述】:

当用户处于给定的位置半径(纬度和经度)时,是否可以在我的 android 应用程序上显示一条消息。我正在使用 Google 地图 API 2。

例如,给定这个纬度和经度 40.218835,-74.000666,如果用户在这个位置的 10 公里半径范围内,那么我想弹出一条消息说“你在水印附近”。例如显示此消息的吐司。

我查看了 Stackoverflow 上的其他一些帖子并查看了有关 radius 的一些教程,但我不知道如何获得我想要的。

感谢任何帮助

【问题讨论】:

    标签: android google-maps location radius


    【解决方案1】:

    是的,有。您使用位置管理器的addProximityAlertBroadcastReveiver

    我会放一些类似于我之前工作的代码的一部分,它应该让你大致了解要搜索的内容......

    // IN SOME CLASS, YOU REGISTER BROADCAST RECEIVER FOR SOME COORDINATES
    Intent thisIntent = new Intent("my.package.name.MyProximityReceiver"); // MyProximityReceiver is the name of the class where you extend BroadcastReceiver
    Bundle thisBundle = new Bundle();
    thisBundle.putString("placeName", "SomePlaceName");
    thisIntent.putExtras(thisBundle);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(getBaseContext(), 0,
            thisIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locManager.addProximityAlert(40.218835,-74.000666, 
            10000 , -1, proximityIntent);  // 10000 is the 10km radius , -1 means no expiration, read more in the link provided above
    
    // END OF THAT SOME CLASS YOU ARE REGISTERING BROADCASTRECEIVER IN
    
    
    // CLASS MyProximityReceiver
    public class MyProximityReceiver extends BroadcastReceiver {
    
          public String placeName;
    
          @Override
          public void onReceive(Context c, Intent intent) {
    
              Log.d("TAG_IN_BROADCAST", "In broadcast receiver");
              String key = LocationManager.KEY_PROXIMITY_ENTERING;
              boolean entering = intent.getBooleanExtra(key, false);
    
              Bundle thisBundle = intent.getExtras();
              if (thisBundle != null)
              {
                  placeName = thisBundle.getString("placeName");
              }
    
              if (entering)
              {
                  Log.e("TAG_ENTERING","ENTERING");
                  // SOMEONE ENTERED THE AREA
              }
              else
              {
                  Log.e("TAG_LEAVING", "LEAVING");
                  // SOMEONE EXITED THE AREA
              }
                  ...
                  ...
          }
          ....
    }
    

    这可能不起作用(这些只是代码的一部分),但这只是为了让您了解如何做您想做的事情......

    希望这会有所帮助...

    【讨论】:

    • 看起来很混乱,但尽管如此,我试一试,如果它完成了工作,你的男人!!!
    【解决方案2】:

    您正在寻找的技术是地理围栏。谷歌官方地理围栏文档请参考this文档。

    关于同样的一个不错的 android 教程,请参阅 this 文档。

    希望这有帮助!

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多