Ayman Emad
Ayman Emad

Reputation: 113

saving current location on GoogleMap V2 by JSON parser

i want to save my location in database , when use Jason parser this error appear in (String name = latlng.getText().toString(); )

( The method getText() is undefined for the type LatLng ) where LatLng from type LatLng

anyone can solve it ??

protected String doInBackground(String... args) {
            String name = latlng.getText().toString();


            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("yout_location", name));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(add_location,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), NamesActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

Upvotes: 0

Views: 207

Answers (3)

someone
someone

Reputation: 427

I have worked on similar project.

If you are trying to store name of the location you need to use geocoder to convert LatLng points into 'formatted_address'.

If you are trying to store Latitude and longitude values in database ,which are of 'double' data type you need to convert them into string as follows

String lat = Double.toString(latlng.latitude);
String lng = Double.toString(latlng.longitude);

UPDATE

Pass LatLng points to asynctask and replace your code with this

protected Void doInBackground(LatLng... params) {
        Log.d("Maps", "background");
        String lat = Double.toString(params[0].latitude);
        String lng = Double.toString(params[0].longitude);
        String strUrl = "http://yourdomain.com/save.php";                   
        URL url = null;
        try {
            url = new URL(strUrl);
            Log.d("Maps", "url fetch");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            Log.d("Maps", "opened");
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                    connection.getOutputStream());

            outputStreamWriter.write("lat=" + lat + "&lng="+lng);               
            Log.d("Maps", "wrote");
            outputStreamWriter.flush();
            outputStreamWriter.close();

            InputStream iStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new
            InputStreamReader(iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";

            while( (line = reader.readLine()) != null){
                sb.append(line);
            }

            reader.close();
            iStream.close();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

Upvotes: 1

kgandroid
kgandroid

Reputation: 5595

I think you want to get the loation using latitude and longitude.you can do that using the following method.

public  String getAddress(Context ctx, double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
            List<address>
 addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);

    String locality=address.getLocality();
    String city=address.getCountryName();
    String region_code=address.getCountryCode();
    String zipcode=address.getPostalCode();
    double lat =address.getLatitude();
    double lon= address.getLongitude();

                result.append(locality+" ");
                result.append(city+" "+ region_code+" ");
    result.append(zipcode);

            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }

To get the latitude and longitude from LatLng object you can do the following:

double lat = latlng.latitude;
double lng = latlng.longitude;

Then just call the method getAddress() within an asynctask like the following:

private class GetCurrentPlace extends AsyncTask<string string="" void=""> {
double lat;
double long;

GetCurrentPlace(double lat,double long)
{
  this.lat=lat;
  this.long=long;
}

  @Override
  protected String doInBackground(String... urls) {
  String address= getAddress(context, lat, long);
   return address;
  }

  @Override
  protected void onPostExecute(String resultString) {
   dialog.dismiss();
   result.setText(resultString);

  }
 }

Call the asynctask as:

new GetCurrentPlace(latlng.latitude,latlng.longitude).execute();

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

Geocoder geocoder = new Geocoder(context);

List<Address> list = null;
try {
   list = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1);
} catch (Throwable e) {
   e.printStackTrace();
}

String name = list.get(0).getSubAdminArea();

Upvotes: 0

Related Questions