-1

I have a URL which contains a lot of LatLng . I have to parse all of them and display marker on Google Map I am new in Android. So please help me.

My JSON URL is here - JSON URL for LatLng

and my code is to fetch data is

 public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    final String TAG = "AsyncTaskParseJson.java";

    // set your json string url here
    String yourJsonStringUrl = "http://djs-corner.appspot.com/getClosestClubs?lat=40.7600624&lon=-73.98558";

    // contacts JSONArray
    JSONArray dataJsonArr = null;

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... arg0) {

        try {

            // instantiate our json parser
            JsonParser jParser = new JsonParser();

            // get json string from url
            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);


            // get the array of users
            dataJsonArr = json.getJSONArray("0");

            // loop through all users
            for (int i = 0; i < dataJsonArr.length(); i++) {

                JSONObject c = dataJsonArr.getJSONObject(i);

                // Storing each json item in variable
                String zip= c.getString("zip");
                String lattitude= c.getString("lat");
                String longitude= c.getString("lng");

                // show the values in our logcat
                Log.e(TAG, "Zip: " + zip
                        + ", Lattitude: " + lat
                        + ", Longitude: " + lng);

            }

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

        return null;
    }

    @Override
    protected void onPostExecute(String strFromDoInBg) {}
}

}

2 Answers2

0

You can pass your lat long values and open maps app externally.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse("http://maps.google.com/maps?saddr=&daddr="
                                    + lattitude+","+longitude));
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    intent.setClassName("com.google.android.apps.maps",
                            "com.google.android.maps.MapsActivity");
                    startActivity(intent);
Jas
  • 3,207
  • 2
  • 15
  • 45
  • Hi thnxx fro quick response but let me tell you, i dont want to open externally by using Intent. data i have to reflect on Google Map – andorid_softy Oct 14 '15 at 06:51
  • You have implemented Google maps api in your app? – Jas Oct 14 '15 at 06:53
  • Yes i did.. but dont know how to parse data and reflect on Map by showing Snippet – andorid_softy Oct 14 '15 at 06:54
  • This might help you http://stackoverflow.com/questions/18532581/how-to-retrieve-current-device-location-show-it-on-map-fragment-in-a-fragment – Jas Oct 14 '15 at 06:54
  • just pass your lat long instead of retrieving current location – Jas Oct 14 '15 at 06:55
  • Thnx Jas but may i ask you how can i accomplish this task by Using JSON. because I don't know all the latitude and longitude. I have to fetch all that from JSON – andorid_softy Oct 14 '15 at 07:02
  • Yes.Retrieve all lat long values and store it in any array and use – Jas Oct 14 '15 at 07:03
0

use these two java and xml files and put your lang lat in given code

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity implements OnMapReadyCallback  {

    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            // Loading map
            initilizeMap();

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

    }
     @Override
        public void onMapReady(GoogleMap map) {
         System.out.println("map ready");
            LatLng delhi = new LatLng(your lan, your lat);

            map.setMyLocationEnabled(true);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(delhi, 7));

            map.addMarker(new MarkerOptions()
                    .title("Sydney")
                    .snippet("The most populous city in Australia.")
                    .position(delhi));
        }

    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
             LatLng latlng = new LatLng(28.38, 77.12);

             googleMap.setMyLocationEnabled(true);
             googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 10));

             googleMap.addMarker(new MarkerOptions()
                        .title("Sydney")
                        .snippet("The most populous city in Australia.")
                        .position(latlng));
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

permissions

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
raj
  • 2,088
  • 14
  • 23
  • HI raj thnxx dear..but may i ask you how can i accomplish this task by Using JSON. because I don't know all the latitude and longitude. I have to fetch all that from JSON – andorid_softy Oct 14 '15 at 07:02
  • If you have parsed the lat and the lon out of the json, start a new activity (or replace a fragment) (with lat and lon as intent extras) with the maps layout. – PatrickMA Oct 14 '15 at 07:16