1

How to get lat ,long from google api and i have some code it works prefectly but some times it is giving us,uk values even i am in india.

String surl = "http://mobilemaps.clients.google.com/glm/mmap";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(surl);
httppost.setEntity(new MyCellIDRequestEntity(shortcid, lac));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
DataInputStream dis = new DataInputStream(entity.getContent());

// Read some prior data
dis.readShort();
dis.readByte();
// Read the error-code
int errorCode = dis.readInt();
System.err.println("Error Code iss::" + errorCode);
int api = myPrefs2.getInt("api", 0);
if (errorCode == 0) 
{
    lat = (double) dis.readInt() / 1000000D;
    lng = (double) dis.readInt() / 1000000D;
    System.err.println("lattitude::" + Double.toString(lng) + "::long::" + Double.toString(lat));
} else {
    // System.err.println("Wrong Error Code");
}
} catch (Exception e) {
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Praveen Kumar
  • 89
  • 2
  • 8
  • Have you tried these before - [Google api](http://stackoverflow.com/q/3574644/940096) & [Latitude & Longtitude using Google api](http://www.google.com/search?q=android+getting+current+position's+latitude+and+longtitude&ie=utf-8&oe=utf-8&aq=t#hl=en&sclient=psy-ab&q=android+getting+latitude+and+longtitude+using+google+api&oq=android+getting+latitude+and+longtitude+using+google+api&aq=f&aqi=q-A2&aql=&gs_l=serp.3..33i29l2.86498.87256.1.88852.2.2.0.0.0.0.183.333.0j2.2.0...0.0.1VrIKxPB5DY&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=1a1f4768cce211b1&biw=1440&bih=789) – Praveenkumar Jun 08 '12 at 05:46
  • No i didn't am getting lat,long with gps n network prefectly,but get st-rucked with google api – Praveen Kumar Jun 13 '12 at 12:41
  • You got the latitude & longtitude? Give me those here.. – Praveenkumar Jun 13 '12 at 12:48

2 Answers2

0

Just go through the link which help you to get Full Address of user location.

Community
  • 1
  • 1
Ashok Domadiya
  • 1,124
  • 13
  • 31
  • Hii Gili thank for ur reply but the thing is i want latitude and latitude of current location with out using gps and network(i.e by using google api) and i just need latitude and longitude only not address and anything.Pls help me to get this – Praveen Kumar Jun 13 '12 at 12:39
0

Try out this -

calling function From Activity :

String location_name = getmylocation(latitude+","+longitude)

getMyLocation() method will provide a string of your location -

public String getmylocation(String value)
{           
      JSONObject json = JSONfunctions.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+value+"&sensor=true");
      try{
        JSONArray  georesult = json.getJSONArray("results");
            JSONObject e = georesult.getJSONObject(0);

            String result = e.getString("address_components");
            JSONArray jArray1 = new JSONArray(result);
            JSONObject json_data=jArray1.getJSONObject(3);

            loc = json_data.getString("long_name");

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

JSONfunctions.java

package com.sample.getcurrentlocation;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

You can simply see the location using String location_name And, don't forget to add required permissions -

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Hii Spk thank for ur reply but the thing is i want latitude and latitude of current location with out using gps and network(i.e by using google api) and i just need latitude and longitude only not address and anything.Pls help me to get this – Praveen Kumar Jun 13 '12 at 12:38