2017-02-27 120 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