【问题标题】:How to load markers dynamically for current position in Android google-maps?如何在 Android google-maps 中为当前位置动态加载标记?
【发布时间】:2010-08-05 09:03:59
【问题描述】:

我目前正在开发一个使用谷歌地图服务的安卓应用。因为用户将能够看到数千个标记,所以我只想加载当前在地图范围内的标记(即当用户查看特定地图图块时)。我知道如何使用 javascript/html 来做到这一点。但是,Android似乎没有提供consesLatLng(latlng)或getBounds等类似的方法(我只找到了getLatitudeSpan和getLongitudeSpan,但不知道如何使用它们才能达到类似的效果)。

谁能给我一些提示?我将非常感谢任何帮助或至少为自己指明正确的方向。

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    您可以使用 getMapCenter 并只需添加或减去纬度跨度或经度跨度(当然除以 2)即可找到边界。

    【讨论】:

    • 感谢 Twaroog 的提示 - 效果很好。多亏了你,我的项目才得以进一步发展。
    • 没问题,我在我的项目中也是这样做的。
    【解决方案2】:

    这些地标是从哪里来的?如果它们来自 Web 服务,请检查 Web 服务是否支持空间查询。如果它是一个基本的范围点(矩形信封),你可以这样做:

    注意:(xmin, ymin -> xmax, ymax) 是矩形的边界(范围)。

    def is_point_in_rect(px, py, xmin, ymin, xmax, ymax):
      if px >= xmin and px <= xmax:
        if py >= ymin and py <= ymax:
          return True
      else:
        return False
    

    这是非常简单的事情。您甚至可以使用 Google Maps Data API 来存储您的地标,并且它具有执行空间查询的机制。

    如果您的数据在 Google App Engine 上,那么您可以使用基于 GeoPt 的查询,或者您可以滚动您自己的空间索引。查看http://code.google.com/p/geomodel/

    【讨论】:

    • 目前它们来自本地 sql 数据库,但将来我也想将其扩展到远程数据库。
    【解决方案3】:

    我正在做与你类似的事情,但我使用的是服务器。我这样做的原因是出于计算原因。当应用程序第一次启动时,一个 HTTP 请求被发送到一个包含 MySQL 查询的 php 脚本。 php 脚本接收用户当前的纬度/经度,并以 JSON 形式返回当前位置 1KM 内的所有点。然后应用程序将该 JSON 解析到本地 MySQL 数据库并对其进行处理。

    要发送 HTTP 请求并将 JSON 解析到数据库中,我正在执行以下操作..

      //http post
     try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://URL");
             httppost.setEntity(new UrlEncodedFormEntity(parameters));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             InputStream is = entity.getContent();
    
     //convert response to string
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
             }
             is.close();
    
             result=sb.toString();
     }catch(Exception e){
         new AlertDialog.Builder(this)
          .setTitle("Woops!")
          .setMessage("Getlocations:It appears the server is not reachable. Check your connectivity and try again.")
          .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    
            public void onClick(DialogInterface dialog,
                    int which) {
                //User wants to start activity..
    
            }
           })
           .show();
             Log.e("log_tag", "Error in connection or convert "+e.toString());
     }
    
     //parse json data
     try{
         System.out.println("parsing data");
             JSONArray jArray = new JSONArray(result);
             if(icount != 0){
                 try{
                     SQLiteDatabase db = tweets.getWritableDatabase();
                     db.delete(TABLE_NAME,null,null);
                 }finally {
                     tweets.close();
                 }
             }
             //TODO: Change this to num
             for(int i=0;i<jArray.length() && q < 1;i++){
                 System.out.println("ARRAY LENGTH " +  jArray.length());
                    q++;
                     JSONObject json_data = jArray.getJSONObject(i);
    
                            System.out.println("Getting Info");
                             Double lat = json_data.getDouble("lat");
                             Double lng = json_data.getDouble("lng");
                             String link = json_data.getString("link");
                             String message = json_data.getString("message");
                             String sname = json_data.getString("sname");
                             String name = json_data.getString("name");
                             //Add each entry to SQL database...
                             System.out.println("Adding table row!");
                             try{
                                 addloc(name,sname,message,link,lat,lng);
                             } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } finally {
                                 tweets.close();
                             }
                             count();
    
    
            }
     }catch(JSONException e){
         new AlertDialog.Builder(this)
          .setTitle("Woops!")
          .setMessage("Getlocations: It appears something went wrong processing the information.")
          .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    
            public void onClick(DialogInterface dialog,
                    int which) {
                //User wants to start activity..
    
            }
           })
           .show();
             Log.e("log_tag", "Error parsing data "+e.toString());
     }
    

    为此,您只需要一个简短的 php 文件,其中包含 Lat / lng 之类的...

      <?php
      mysql_connect("host","user","pass");
      mysql_select_db("database");
      $lat = $_REQUEST['currlat'];
      $lng = $_REQUEST['currlng'];
      $q=mysql_query("QUERY") or die(mysql_error());
      if (mysql_num_rows($q) == 0) {
     echo "No rows found, nothing to print so am exiting";
    exit;
     }
    if(!q){
    echo "NO RESULT";
    exit;
    }
     while($e=mysql_fetch_assoc($q))
        $output[]=$e;
     print(json_encode($output));
    
     mysql_close();
     ?>
    

    这将接受值并以 JSON 格式打印结果。

    //编辑:首先我使用 GPS 来获取 lat / lng。

    希望这会有所帮助。

    【讨论】:

    • 有趣的艾丹克。这确实很有帮助。非常感谢您分享此代码!
    • 没问题,请注意在我的 php 中我没有使用准备好的语句。如果你要制作这个生产代码,你需要稍微修改一下。
    【解决方案4】:

    答案取决于您的标记来自何处。它们是存储在 Android 设备本身还是来自网络服务器? 无论哪种方式,如果您需要同时表示数千个位置,您可能需要实现某种形式的集群,如果标记来自网络,则可以在客户端或服务器上完成。

    例如,请参阅此服务器端集群器(2 个不同版本):

    http://maps.forum.nu/server_side_clusterer

    http://maps.forum.nu/server_side_clusterer/index2.php

    或客户端集群:

    http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/

    另一种选择是使用自定义图块(如 Google 所做的那样),但根据您的确切需求,这可能是矫枉过正。

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2014-11-23
      • 2016-08-22
      • 1970-01-01
      • 2015-01-26
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多