简体   繁体   中英

Markers don't show on Google Maps v2

I'm and Android newbie and I'm with some difficult getting a Google Maps v2 markers to display. If I put a single test marker, with some hardcoded values in it it works pretty well. However, my code doesn't seem to work.

I've already debuged and saw the values are correct and entering the function, but I can't get it to work.

I'm also debugging to a physical device.

Here's the code

private void inflateGoogleMap(ArrayList<Geocache> cacheList){
    GoogleMap map = ((MapFragment) getFragmentManager()

            .findFragmentById(R.id.map)).getMap();

        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));


        for(int i=0; i<cacheList.size(); i++){
            Log.d("CACHE_NAME", cacheList.get(i).getName()); 
            Log.d("CACHE_LAT", Double.toString(cacheList.get(i).getLatitude()));
            Log.d("CACHE_LONG",  Double.toString(cacheList.get(i).getLongitude()));

            Double cacheLat = cacheList.get(i).getLatitude();
            Double cacheLong = cacheList.get(i).getLongitude();
            LatLng pos = new LatLng(cacheLat, cacheLong);
            map.addMarker(new MarkerOptions().position(pos));

                    //Log.d("CACHE_TYPE", json.getString("type"));
                    //Log.d("CACHE_REGION", json.getString("region"));
        /*Marker marker =   map.addMarker(new MarkerOptions()
                .title(cacheList.get(i).getName())
                .snippet(cacheList.get(i).getName())
                .position(new LatLng(cacheList.get(i).getLatitude(), cacheList.get(i).getLongitude())));*/

        }
}

How I get the Json list

public void getNearbyGeocaches(double lat, double lon,int limit){
    //String url = "http://test.opencaching.com/api/geocache?center=38.642%2c-94.754&limit=5&Authorization="+API_KEY;

    String url = "http://test.opencaching.com/api/geocache?center="+lat+"%2c"+lon+"&limit="+limit+"&Authorization="+super.getApiKey();

    super.get(url, null, new JsonHttpResponseHandler() {
        @Override

        public void onSuccess(JSONArray response) {
            // Pull out the first event on the public timeline
            JSONObject json = null;
            ArrayList<Geocache> geoList = new ArrayList<Geocache>();
            //Log.d("JSON_LENGTH",Integer.toString(timeline.length()));

            for(int i = 0; i<response.length(); i++)
            {
                try {
                    //Cast response para JSONObject
                    json = (JSONObject) response.get(i);

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

                try {

                    Geocache cache = new Geocache();
                    //Log.d("CACHE_NAME", json.getString("name"));
                    //Log.d("CACHE_TYPE", json.getString("type"));
                    //Log.d("CACHE_REGION", json.getString("region"));

                    cache.setName(json.getString("name"));
                    cache.setDescription(json.getString("description"));
                    JSONObject location = json.getJSONObject("location");
                    cache.setLatitude(Double.parseDouble(location.getString("lon")));
                    cache.setLongitude(Double.parseDouble(location.getString("lat")));
                    //Log.d("CACHE_LAT", location.getString("latitude"));
                    //Log.d("CACHE_LONG", location.getString("longitude"));
                    //cache.setName(firstEvent.getString("name"));
                    geoList.add(cache);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                //
            }

            inflateGoogleMap(geoList);

            // Do something with the response

        }
    }); 


}

Thanks for the help!

I noticed that I switched the Latitude and Longitude when creating the marker.

I was passing latitutde a 151º value, when the ranges are from -90º to 90º

Add marker this way..

Mmap.addMarker(new MarkerOptions().position(pos).title(ad).snippet(details).icon(BitmapDescriptorFactory.fromResource(markericon)));

where pos is LatLng(lat,lon) and title is the title that is to be displayed in the snippet and details is the description that is to be shown on the snippet and markericon is the marker icon that is to be displayed.

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