2016-02-23 147 views
0

因此,在解決我的問題Converting JSONObject to JSONArray後,我能夠從JSON文件中獲得Lat,Long。但是,我似乎無法讓標記顯示。谷歌地圖標記不出現(Android)

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

JSONParser jsonParser = new JSONParser(); 
private GoogleMap mMap; 
private GoogleMap nMap; 
private static final LatLng Tampines = new LatLng(1.3525,103.9446); 
List<TaxiCoordinates> myCoordinates = new ArrayList<TaxiCoordinates>(); 
private ProgressDialog pDialog; 
MapsActivity map; 
private static final String apiURL = "http://datamall2.mytransport.sg/ltaodataservice/TaxiAvailability/"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    MapFragment mapFragment = (MapFragment) getFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    myCoordinates = new ArrayList<TaxiCoordinates>(); 
    new GetCoordinates().execute(); 

} 

/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Tampines, 10)); 




    // Add a marker in Sydney and move the camera 
    // LatLng sydney = new LatLng(-34, 151); 
    // mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    // mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

class GetCoordinates extends AsyncTask<String, String, JSONArray> { 
    private ProgressDialog progressDialog; 

    @Override 
    protected JSONArray doInBackground(String... params) 
    { 
     try { 
      JSONArray json = jsonParser.makeHttpRequest(
        apiURL, "GET"); 

      if (json != null) { 
       Log.d("JSON result", json.toString()); 

       return json; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(MapsActivity.this); 
     progressDialog.setMessage("Loading Taxis..."); 
     progressDialog.show(); 
    } 

    @Override 
    protected void onPostExecute(JSONArray jsonArray) { 

     if (jsonArray != null) 
     { 
      //loop 
      for(int i = 0; i < jsonArray.length(); i++) 
      { 
       try 
       { 
        JSONObject c = jsonArray.getJSONObject(i); 
        TaxiCoordinates temp = new TaxiCoordinates(); 
        temp.setLatitude(c.getDouble("Latitude")); 
        temp.setLongitude(c.getDouble("Longitude")); 
        myCoordinates.add(temp); 
        if(progressDialog!=null && progressDialog.isShowing()) 
        { 
         progressDialog.dismiss(); 
        } 

       } 
       catch (JSONException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      addMarkers(); 
     } 
    } 
} 



public void addMarkers() 
{ 
    for(TaxiCoordinates c: myCoordinates) 
    { 
     double tempLat = c.getLatitude(); 
     double tempLong = c.getLongitude(); 

     LatLng taxi = new LatLng(tempLat, tempLong); 
     mMap.addMarker(new MarkerOptions() 
       .position(taxi) 
       .title("Available")); 

     //MarkerOptions m = new MarkerOptions().title("Available").position(new LatLng(tempLat, tempLong)); 
     //googleMap.addMarker(m); 
    } 
} 
} 

順便說一句,我試圖把代碼addMarkers()到適當的onMapReady(),它仍然沒有顯示出來。

編輯:我想這:

mMap.addMarker(new MarkerOptions().position(tempLat,tempLng).title(Available).snippet(details).icon(BitmapDescriptorFactory.fromResource(markericon))); //tried default ones too. 
+0

請貼上你的onMapReady回調方法。 –

+0

我認爲這是問題所在。我沒有一個onMapReadyCallBack方法。除非你指的是我已經擁有的onMapReady()。 – Kei

+0

解決了它。我當時很傻。 – Kei

回答

0

沒關係,我是個傻瓜。這樣做時我錯過了一個部分。

mMap.addMarker(new MarkerOptions().position(tempLat,tempLng).title(Available).snippet(details).icon( .fromResource(markericon))); //tried default ones too.