Android谷歌地图标记赢了不显示

时间:2017-02-27 06:16:38

标签: android google-maps gson okhttp3

我是android开发的新手。我试图在我的应用地图上添加Google标记,但它不会显示。我可以设置一个Marker,如果Marker的lat& amp; lng是一个双数字,但当我把API数据放入其中时它不会显示。任何建议?非常感谢。

@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();
}

}

2 个答案:

答案 0 :(得分:0)

地图是否显示出来?你的内心是什么?      的getLocation(); 功能,如果没有以下内容,则添加

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

还要记住放大和缩小并平移地图,可能是标记被绘制在你没想到的地方。 如果它仍然不起作用,请遵循本教程,它必须工作。 google maps with marker

答案 1 :(得分:0)

你确定你从json获得了正确的价值吗?它可能正在返回一个字符串。您可能想尝试将其转换为valueOf以获得所需的类型。

对于双人而言,这将是:

double lat = Double.valueOf(jsonBean.getResult().get(0).getDlat());
相关问题