0

I am very new to JAVA programming, but I am in a big trouble and this will depend on my job. Anyway. Okay so I have this json here:

{
"GetPOIByTypeResult": [
    {
        "Address": {
            "City": "Bucuresti",
            "ID": 1,
            "Number": 3,
            "Street": "Otopeni"
        },
        "ID": 1,
        "IsSpecial": true,
        "Latitude": 44.542869567871094,
        "Logo": "6fb43083ee663364ef6771293653e56b.jpg",
        "Longitude": 26.06893539428711,
        "Name": "Spitalul Judetean",
        "Phone": "085228291",
        "Remark": "Normal Hospital",
        "Schedule": "Monday to Friday",
        "Specialities": [
            {
                "ID": 1,
                "Name": "Stomatologie"
            },
            {
                "ID": 2,
                "Name": "Radiologie"
            },
            {
                "ID": 3,
                "Name": "Cardiologie"
            },
            {
                "ID": 4,
                "Name": "Ginecologie"
            },
            {
                "ID": 5,
                "Name": "Pediatrie"
            },
            {
                "ID": 6,
                "Name": "Patologie"
            },
            {
                "ID": 7,
                "Name": "Oncologie"
            },
            {
                "ID": 8,
                "Name": "Macelarie"
            },
            {
                "ID": 9,
                "Name": "Oftalmologie"
            },
            {
                "ID": 10,
                "Name": "Ghipsologie"
            }
        ],
        "Type": "Hospital"
    },

I need to get the latitude and longitude from the JSON and show the location on google maps in my application. I have this little code snippet over here, it works great with local coordinates, like entering my own numbers, but I want it to get it from JSON.

drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point2 = new GeoPoint((int)(44.55332946777344*1E6),(int)(26.07666015625*1E6));
    CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2, "Bucuresti Spital 2", 
            "(Spitalul 2)", 
            "http://ia.media-imdb.com/images/M/MV5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNjExNTgzNA@@._V1._SX40_CR0,0,40,54_.jpg");       
    itemizedOverlay.addOverlay(overlayItem2);</code>

I have a JSONparser set up,I just don't know how to make the mapcontroller to animateTo the point where my coordinates are,so I would like to get some help with this :-S

EDIT: This is how I resolved my problem, maybe it will help some1 else too.

drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(
            drawable, mapView); //showing marker on the map
public void showHospitalList(ArrayList<Hospitals> hospitals) {

    for (int i = 0; i < hospitals.size(); i++) {

        Hospitals hospital = hospitals.get(i);
        CustomOverlayItem temp1 = toOverlay(hospital);
        itemizedOverlay.addOverlay(temp1);
    }
    mapOverlays.add(itemizedOverlay); //this is to get the list in the activity
}

public CustomOverlayItem toOverlay(Hospitals hospitals) {

    GeoPoint point = new GeoPoint((int) (hospitals.Latitude * 1E6),
            (int) (hospitals.Longitude * 1E6));

    CustomOverlayItem temp = new CustomOverlayItem(
            point, hospitals.Name, hospitals.Phone, hospitals.Type);

    return temp;
// used the hospitals object to define the cordinates and then multiplied by 1E6 to make it compatible(from double to int)  
// Last line defines the balloon pop-up and what to show(everything is from json).
}  

Thank you for helping

SethPDA
  • 62
  • 1
  • 8
  • You seems to be asking two questions here. 1) Parsing JSON to get lat/long. 2) Animate to that location. Try not to mix it up so you can understand better. You parse JSON and store lat/long in an object then use that object when animating the map. Good luck :) – RobGThai Jul 23 '12 at 09:05
  • I am new to Android programming, could you give me an example how to store lat/long in an object and use it in my code above please? – SethPDA Jul 23 '12 at 09:13

1 Answers1

0

Processing JSON. You can either use some library such as Gson or Jackson. For more info: Sending and Parsing JSON Objects . Or you can parse them yourself by using JSONObject.

String jsonString = ... // Your JSON
List<GeoPoint> locationList = new ArrayList<GeoPoint>();
Double lat, lng; // temp
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    lat = jsonObject.getDouble("Latitude");
    lng = jsonObject.getDouble("Longtitude");
    locationList.add(
        new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
}

Then handling map.

MapView mapView = (MapView) findViewById(R.id.map);

MapController mc = mapView.getController();
// Set Zoom level of this map (1-21).
mc.setZoom(21);
int x = 0; // Position to get location
GeoPoint point2 = locationList.get(x); // Get Object from list
// This call handle animation to move map to display the given location
mc.animateTo(point2);
mapView.postInvalidate();
Community
  • 1
  • 1
RobGThai
  • 5,937
  • 8
  • 41
  • 59
  • How to modify this line, so that it gets lat/long from json? This is my biggest question. I am very new to android programming, please help. `code` GeoPoint point2 = new GeoPoint((int)(44.55332946777344*1E6),(int)(26.07666015625*1E6)); `code` – SethPDA Jul 23 '12 at 09:12
  • Thank you very much, I actually resolved my problem elsehow, but the main idea was based on your code, so thank you very much :) – SethPDA Jul 24 '12 at 12:31