2010-08-05 103 views
5

我目前正在开发一款使用Google地图服务的Android应用。由于用户将能够看到数千个标记,因此我只想加载当前处于地图边界内的标记(即,当用户查看特定的地图图块时)。我知道如何用javascript/html做到这一点。然而,Android似乎没有提供任何类似的方法,比如containsLatLng(latlng)或getBounds(我只能找到getLatitudeSpan和getLongitudeSpan,但不知道如何使用它们才能获得类似的效果)。如何在Android谷歌地图中为当前位置动态加载标记?

任何人都可以给我一些提示吗?我会非常感谢任何帮助,或者至少指向正确的方向。

回答

7

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

+0

谢谢你的提示Twaroog - 它工作得很好。感谢你,我能够进一步推进我的项目。 – Pavel 2010-08-05 09:38:15

+0

没问题,我在项目中也是这样做的。 – LordTwaroog 2010-08-05 10:18:32

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地图数据API来存储您的地点标记,并且具有执行空间查询的机制。

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

+0

他们来自Rahul当时的本地SQL数据库,但将来我想将其扩展到远程数据库。 – Pavel 2010-08-05 09:41:51

2

我正在做类似于你自己的事情,但我正在使用服务器。我这样做的原因是出于计算原因。当应用程序首先启动一个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文件这需要纬度/经度东西像...

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

希望这会有所帮助。

+0

有趣的艾丹克。这确实是非常有帮助的。非常感谢您分享此代码! – Pavel 2010-08-05 09:45:40

+0

没问题,在我的PHP注意我没有使用准备好的语句。如果你要制作这个生产代码,你需要敲一下这个代码。 – Aidanc 2010-08-05 09:47:39

2

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

见,例如该服务器侧聚类器(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一样),但取决于你的确切需求可能会过度杀伤。

相关问题