2017-02-17 67 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

相關問題