2016-02-03 42 views
0

我正在運行一些Web服務與一些JSON數據,我用它在我的地圖上做標記(這得到每小時更新)。我想添加按鈕在我的Android地圖,以便我會刷新標記data.any的想法,而不改變大部分結構?我應該在線程上做些什麼?或者重新開始活動?刷新Android上的地圖標記與按鈕

繼承人是代碼

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

private static final String SERVICE_URL = "http://7a27183e.ngrok.com"; 

public GoogleMap map; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(activity_maps); 



} 



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

private void setUpMapIfNeeded() { 
    if (map == null) { 
     MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map); 
     map = mapFragment.getMap(); 
     if (map != null) { 
      setUpMap(); 
      // new MarkerTask().execute(); 
     } 
    } 
} 

private void setUpMap() { 
    UiSettings settings = map.getUiSettings(); 
    settings.setZoomControlsEnabled(true); 
    settings.setScrollGesturesEnabled(true); 
    // 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 retrive 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. 
     //.title(jsonObj.getString("pollutant")+" "+jsonObj.getString("network")) 
     // .snippet(Integer.toString(jsonObj.getInt("numeric_val"))) 
     //DATE!! 
     JSONObject jsonObj = jsonArray.getJSONObject(i); 

     map.addMarker(new MarkerOptions() 
         .title(jsonObj.getString("network") + "\n" + jsonObj.getString("date")) 
         .snippet(jsonObj.getString("pollutant") + "=" + jsonObj.getString("numeric_val")) 

         .position(new LatLng(
           jsonObj.getDouble("x"), 
           jsonObj.getDouble("y"))) 
         .icon(BitmapDescriptorFactory.defaultMarker(new Random().nextInt(360))) 
     ); 


     map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

      @Override 
      public View getInfoContents(Marker arg0) { 
       return null; 
      } 

      @Override 
      public View getInfoWindow(Marker arg0) { 

       View v = getLayoutInflater().inflate(R.layout.customlayout, null); 

       TextView tTitle = (TextView) v.findViewById(R.id.title); 

       TextView tSnippet = (TextView) v.findViewById(R.id.snippet); 

       tTitle.setText(arg0.getTitle()); 

       tSnippet.setText(arg0.getSnippet()); 

       return v; 

      } 
     }); 
    } 



} 

}

這是JSON結構:

https://gist.githubusercontent.com/anonymous/42af315ab003ab01764d/raw/79b6cf5451038bd2e35c376766e9ab44bd385a02/gistfile2.txt

和截圖:

http://imgur.com/WZNC9Oz

回答

0

我已在您的方法中做了一些修改createMarkersFromJson()在線map.addMarker()。現在您可以使用changeMarkerPosition()更改標記的位置。

HashMap<String, Marker> markerHashMap = new HashMap<>(); 


void changeMarkerPosition(String key, LatLng latLng) { 
markerHashMap.get(key).setPosition(latLng); 
} 


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. 
    //.title(jsonObj.getString("pollutant")+" "+jsonObj.getString("network")) 
    // .snippet(Integer.toString(jsonObj.getInt("numeric_val"))) 
    //DATE!! 
    JSONObject jsonObj = jsonArray.getJSONObject(i); 

    markerHashMap.put("key"+i,(map.addMarker(new MarkerOptions() 
        .title(jsonObj.getString("network") + "\n" + jsonObj.getString("date")) 
        .snippet(jsonObj.getString("pollutant") + "=" + jsonObj.getString("numeric_val")) 

        .position(new LatLng(
          jsonObj.getDouble("x"), 
          jsonObj.getDouble("y"))) 
        .icon(BitmapDescriptorFactory.defaultMarker(new Random().nextInt(360))) 
    );) 


    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

     @Override 
     public View getInfoContents(Marker arg0) { 
      return null; 
     } 

     @Override 
     public View getInfoWindow(Marker arg0) { 

      View v = getLayoutInflater().inflate(R.layout.customlayout, null); 

      TextView tTitle = (TextView) v.findViewById(R.id.title); 

      TextView tSnippet = (TextView) v.findViewById(R.id.snippet); 

      tTitle.setText(arg0.getTitle()); 

      tSnippet.setText(arg0.getSnippet()); 

      return v; 

     } 
    }); 
} 
+0

謝謝您的回答!基本上,我想刪除目前的標記,使他們從一開始這就是我的問題... – mikaeloN

+0

你可以簡單地在這種情況下調用marker.remove()。 –

+0

我可以使用相同的修改來根據用戶輸入搜索標記嗎? – mikaeloN