2017-02-17 63 views
-1

我在android工作室中建立一个应用程序基本上是一种应用程序,它会向用户显示许多标记,标记也由用户创建。Android标记从数据库使用PHP和MySQL什么是最好的方法

基本上,我第一次使用android studio,并且我不想搞砸东西,并在这里发布一个问题,这样我就可以在开发代码的过程中获得体验的声音并做好预防措施。

我的应用程序将使用PHP页面来保存数据,并获取从MySQL数据库中的数据,我只是想知道,因为用户将增加这样做的指针是什么调用这些指针在我的应用程序的最佳方法,

Json? 还是有其他方法可以做到这一点,考虑到会有很多标记的最佳做法。

将标记限制到窗口视图是否是一个好主意,代码的增加会增加服务器的负载或在屏幕移动时通常需要多少时间来加载更多数据。

如果我同时加载所有的标记,是否有机会滞后和利用手机和服务器的资源?

我相信你们会引导我尽我所能使用的最佳做法谢谢你。

回答

0

您可以使用经纬度值的JSON数据:

[ 
    { 
     "name": "New York", 
     "latlng": [ 
      62.457434, 
      17.349869 
     ], 
     "population": "123" 
    }, 
    { 
     "name": "Dhaka", 
     "latlng": [ 
      62.455102, 
      17.345599 
     ], 
     "population": "132" 
    }, 
] 

最好是地图加载过程中加载的所有标记数据,因为如果u要加载基于用户的地图上的位置数据的改变,它会调用网络API每次都会在地图上使用触摸,这会拖延UI。

您可以加载所有数据在工作线程中,因为它是网络操作。所以用户界面不会很慢。

这里是一个示例代码:

public class MainActivity extends FragmentActivity { 
    private static final String LOG_TAG = "ExampleApp"; 

    private static final String SERVICE_URL = "YOUR SERVICE URL"; 

    protected GoogleMap map; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

    private void setUpMapIfNeeded() { 
     if (map == null) { 
      map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      if (map != null) { 
       setUpMap(); 
      } 
     } 
    } 

    private void setUpMap() { 
     // Retrieve the city data from the web service 
     // In a worker thread since it's a network operation. 
     new Thread(new Runnable() { 
      public void run() { 
       try { 
        retrieveAndAddCities(); 
       } catch (IOException e) { 
        Log.e(LOG_TAG, "Cannot retrieve cities", e); 
        return; 
       } 
      } 
     }).start(); 
    } 

    protected void retrieveAndAddCities() throws IOException { 
     HttpURLConnection conn = null; 
     final StringBuilder json = new StringBuilder(); 
     try { 
      // Connect to the web service 
      URL url = new URL(SERVICE_URL); 
      conn = (HttpURLConnection) url.openConnection(); 
      InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

      // Read the JSON data into the StringBuilder 
      int read; 
      char[] buff = new char[1024]; 
      while ((read = in.read(buff)) != -1) { 
       json.append(buff, 0, read); 
      } 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error connecting to service", e); 
      throw new IOException("Error connecting to service", e); 
     } finally { 
      if (conn != null) { 
       conn.disconnect(); 
      } 
     } 

     // Create markers for the city data. 
     // Must run this on the UI thread since it's a UI operation. 
     runOnUiThread(new Runnable() { 
      public void run() { 
       try { 
        createMarkersFromJson(json.toString()); 
       } catch (JSONException e) { 
        Log.e(LOG_TAG, "Error processing JSON", e); 
       } 
      } 
     }); 
    } 

    void createMarkersFromJson(String json) throws JSONException { 
     // De-serialize the JSON string into an array of city objects 
     JSONArray jsonArray = new JSONArray(json); 
     for (int i = 0; i < jsonArray.length(); i++) { 
      // Create a marker for each city in the JSON data. 
      JSONObject jsonObj = jsonArray.getJSONObject(i); 
      map.addMarker(new MarkerOptions() 
       .title(jsonObj.getString("name")) 
       .snippet(Integer.toString(jsonObj.getInt("population"))) 
       .position(new LatLng(
         jsonObj.getJSONArray("latlng").getDouble(0), 
         jsonObj.getJSONArray("latlng").getDouble(1) 
       )) 
      ); 
     } 
    } 
} 
+0

@Ubaid拉赫曼回答没有帮助? – rafsanahmad007

相关问题