2017-02-27 117 views
0

我是android開發新手。我試圖在我的應用地圖中添加Google標記,但不會顯示。我可以設置一個標記,如果標記的lat是一個雙精度數,但是當我將API數據放入它時,它不會顯示。任何建議?非常感謝。Android谷歌地圖標記不會顯示

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    this.googleMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    getLocation(); 

    // This marker will show at my app screen ! 
    LatLng latLng = new LatLng(24.9992666, 121.5082287); 
    googleMap.addMarker(new MarkerOptions().position(latLng) 
      .title("This is office Marker") 
    ); 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 





    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    this.googleMap.setMyLocationEnabled(true); 




    HttpUtil.sendHttpRequest("http://113.10.198.159/appapi/getWIFIList", new HttpCallbackListener() { 
     @Override 
     public void onFinish(String response) { 
      Gson gson = new Gson(); 
      JsonBean jsonBean = gson.fromJson(response, JsonBean.class); 

      Log.d(TAG, "【】【id】【】" + jsonBean.getResult().get(0).getId()); 
      Log.d(TAG, "【】【merchant_id】【】" + jsonBean.getResult().get(0).getMerchant_id()); 
      Log.d(TAG, "【】【merchant_name】【】" + jsonBean.getResult().get(0).getMerchant_name()); 
      Log.d(TAG, "【】【city】【】" + jsonBean.getResult().get(0).getCity()); 
      Log.d(TAG, "【】【area】【】" + jsonBean.getResult().get(0).getArea()); 
      Log.d(TAG, "【】【address】【】" + jsonBean.getResult().get(0).getAddress()); 
      Log.d(TAG, "【】【lat】【】" + jsonBean.getResult().get(0).getLat()); 
      Log.d(TAG, "【】【lng】【】" + jsonBean.getResult().get(0).getLng()); 
      Log.d(TAG, "【】【addTime】【】" + jsonBean.getResult().get(0).getAddTime()); 
      Log.d(TAG, "【】【dlat】【】" + jsonBean.getResult().get(0).getDlat()); 
      Log.d(TAG, "【】【dlng】【】" + jsonBean.getResult().get(0).getDlng()); 
      Log.d(TAG, "【】【wificode】【】" + jsonBean.getResult().get(0).getWificode()); 
      Log.d(TAG, "【】【upstream】【】" + jsonBean.getResult().get(0).getUpstream()); 
      Log.d(TAG, "【】【downstream】【】" + jsonBean.getResult().get(0).getDownstream()); 

      //// This marker can not show at my app screen 
      LatLng latLng = new LatLng(jsonBean.getResult().get(0).getDlat(), jsonBean.getResult().get(0).getDlng()); 
      Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng) 
        .title("This is Test Marker") 
      ); 

     } 

     @Override 
     public void onError(Exception e) { 

     } 
    }); 
} 



public class HttpUtil { 
public static void sendHttpRequest(final String address, final HttpCallbackListener listener) { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       OkHttpClient client = new OkHttpClient(); 
       String s1 = "lat"; 
       String s2 = "24.9992666"; 
       String s3 = "lng"; 
       String s4 = "121.5082287"; 
       RequestBody requestBody = new FormBody.Builder() 
         .add(s1, s2) 
         .add(s3, s4) 
         .build(); 
       Request request = new Request.Builder() 
         .url(address) 
         .post(requestBody) //post 
         .build(); 
       Response response = client.newCall(request).execute(); 
       String responseData = response.body().string(); 
       if (listener != null) { 
        // onFinish() method 
        listener.onFinish(responseData); 
       } 
      } catch (Exception e) { 
       if (listener != null) { 
        // onError() method 
        listener.onError(e); 
       } 

      } 
     } 
    }).start(); 
} 

}

+0

代碼請。謝謝。 http://stackoverflow.com/help/mcve –

+0

請添加代碼,以獲得想法你嘗試過什麼? –

+0

@SouravGanguly這是我的代碼,這是我使用Stackoverflow時的第一個問題,對不起,我是一個新人。 – JackenLiu

回答

0

是地圖shoiwing嗎?什麼是你的 getLocation(); 功能,如果沒有類似以下內容,然後將其添加

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

還記得放大和縮小和周圍平移地圖,可能是標誌繪製,你沒有想到它是。 如果它仍然不起作用,請按照本教程,它必須工作。 google maps with marker

+0

是的地圖也顯示出來了。我可以在地圖屏幕上看到第一個標記,但第二個標記不能顯示。這真的殺了我。 – JackenLiu

+0

您可以發佈您的整個mainActivity.java代碼,以及從http://113.10.198.159/appapi/getWIFIList收到的結果是什麼,當我嘗試它時,我幾乎什麼也沒有,確保它接收到合適的座標,解析它們之前加倍試圖在地圖上繪製它們,如果仍然不起作用,請仔細檢查LOGCAT日誌,並查看此鏈接https://gist.github.com/saxman/5347195 – burgur

+0

謝謝!但我已經解決了,我把問題代碼放在處理程序中,它已經工作了!不管怎樣,謝謝。 – JackenLiu

0

你確定你從json獲得了正確的值嗎?它可能會返回一個字符串。您可能想嘗試將其轉換爲valueOf以獲得所需的類型。

對於雙會是這樣的:

double lat = Double.valueOf(jsonBean.getResult().get(0).getDlat()); 
+0

是的數據類型是真的,我用郵遞員檢查是。當我調試應用程序時,第二個Marker的lat&lng都有價值,但它仍然無法顯示在屏幕上。此外,Android Studio還顯示了Handler的應用程序調試問題。 – JackenLiu