简体   繁体   中英

Android google map Marker won't Show

I'm new at android development. I'm trying to add a Google marker at my App map but it won't show. I can set a Marker if the Marker's lat & lng is a double number,but when i put the API data in it it won't show.Any suggestion? Thanks a lot.

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

}

Is the map shoiwing up? What is inside your getLocation(); function, if there is not something like the following, then add it

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

Also remember to zoom in and out and pan the map around, may be the marker is plotted where you did not expect it to be. if it still does not work, follow this tutorial, it must work. google maps with marker

Are you sure you're getting the right value from the json? It may be returning a string. You might want to try converting it to the valueOf to get the desired type.

For a double it would be something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM