Adding latitude and Longitude into a Google Places URL in Android - android

I'm working on finding nearby places in my Android app. I can find the current latitude and longitude as a double and then converting the double into a string to display in a textview (just to prove to myself that it's working). The problem I'm having is passing the lat. & long. strings into the Places URL. The URL displays the JSON data when I use a fixed value string for the lat. and long., but it returns INVALID_REQUEST when I try to pass the obtained doubles converted into strings into the URL. Here's what it looks like:
Location currentLocation;
double currLatitude;
double currLongitude;
String latString;
String longiString;
String latString2 = "30.4335320";
String longiString2 = "-97.9822360";
towers = locMan.getBestProvider(criteria, true);
currentLocation = locMan.getLastKnownLocation(towers);
currLatitude = currentLocation.getLatitude();
currLongitude = currentLocation.getLongitude();
latString = String.valueOf(currLatitude);
longiString = String.valueOf(currLongitude);
latiText.setText(latString);
longiText.setText(longiString);
I think maybe I'm not parsing the doubles into strings correctly here?
latString = String.valueOf(currLatitude);
longiString = String.valueOf(currLongitude);
because in the below URL when I use the parsed values latString, longiString that's when I get INVALID_REQUEST, but if I pass latString2, longiString2, which are given values, the URL displays the JSON data. Here's the URL:
String tURL="https://maps.googleapis.com/maps/api/place/search/json?"
+ "location=" + latString + "," + longiString
+ "&radius=25000&"
+ "types=church&name=baptist&sensor=false&key="
+ myPlaceKey;

I fugured it out. My problem was that the activity that returned the URL was a separate Activity from the URL string builder. And the URL returning Activity did not call the method where I obtained the latitude and longitude, so I think the URL the returning Activity obtained basically had "null" for the coordinates. So I combined the two Activities so now the coordinates are obtained, then the URL is built, then the URL result is displayed in a textview, all in one Activity - and it WORKS! I'm such an idiot!

Related

how to pass latitude and longitude in retrofit for nearest road API

this is the request format
https://roads.googleapis.com/v1/nearestRoads?parameters&key=YOUR_API_KEY
here i need to pass latitude and longitude as parameter points, something like this
https://roads.googleapis.com/v1/nearestRoads?points=60.170880,24.942795|60.170879,24.942796|60.170877,24.942796&key=YOUR_API_KEY
i tried to pass it like
#POST("nearestRoads?points&key=my_api_key")
Call<SnappedPoints> getNearestRoad(#FieldMap Double map);
by calling
Call<SnappedPoints> call = retrofitClientMap.getNearestRoad(stringStringMap.put(latLng3.latitude,latLng3.longitude));
but it shows an illegal exception , can any one help have an idea to solve it
Have you tried something like this
#POST("nearestRoads?key=my_api_key")
Call<SnappedPoints> getNearestRoad(#Field("points") String points);
then pass your points as String to this method
i got it by calling api service like
#POST("nearestRoads?key=my_api_key")
// Call<MySnappedPoints> getNearestRoad();
Call<MySnappedPoints> getNearestRoad(#Query("points") String points);
calling it by
String test = Double.toString(mGoogleMap.getCameraPosition().target.latitude) + "," + Double.toString(mGoogleMap.getCameraPosition().target.longitude);
Call<MySnappedPoints> call = retrofitClientMap.getNearestRoad(test);

how to get the google map url of my current location in Android?

I'm trying to get the exact URL of my current location showing my gps location as a marker on the map.
You could use a link in this format:
https://www.google.com/maps/search/?api=1&query=36.26577,-92.54324
and add the latitude and longitude of your location in the place of 36.26577 and -92.54324 respectively.
http://maps.google.com/?q=<lat>,<lng>
Use the above format, pass your values of lat and lng to the above URL format.
You can use it with android Intent as.
String geoUri = "http://maps.google.com/maps?q=loc:" + <lat-long>;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
startActivity(intent);
or simply URL
"http://maps.google.com/maps?q=loc:" + <lat-long>;

Why doesn't my regex code get this part of the string?

I'm making an app that shows the user the nearest gyms. After I put in a request to the Google Places Api, it returns me some json. I get the latitude from that json using regex. However, I cannot get the longitude. My code is below. Can someone help please?
for (int i = 0; i < jsonArray.length(); i++){
String latitude = "\"lat\":";
String longitude = "lng\":";
Pattern p = Pattern.compile(latitude +"(.*?),");
Pattern pattern = Pattern.compile(longitude + "(.*?)"+ ",");
JSONObject jsonPart = jsonArray.getJSONObject(i);
String latLong = jsonPart.getString("geometry");
Matcher m = p.matcher(jsonPart.getString("geometry"));
Matcher matcher = pattern.matcher(jsonPart.getString("geometry"));
while( matcher.find()) {
Log.i("longitude", matcher.group(1));
}
while (m.find()){
Log.i("latitude", m.group(1));
}
}
This is the line I need to get the latitude and longitude from. I can get the latitude, but longitude doesn't work.
gymlocation: {"location":{"lat":40.4434584,"lng":-79.964227}}
String longitude = "\"lng\":";
You are missing \"
Alternatively, isn't this better?
Geting json response from Google places API in android
If you still want to use regex, your second expression "lng"(.*?), isn't finding anything because of the ? lazy operator. It will try to match at least characters as possible , and as the string you're searching on has no , but your expression tries to match one at the end, it will just not match anything.

How to extract lat and long values from a google maps link

I have a web view that shows google maps on my android activity. I am currently getting the link that contains the latitude and longitude values of the selected location by clicking a button. What I want to know is, how can I extract and save the latitude and longitude values in to variables? I tried this code but it doesn't seem to work
Uri uri = Uri.parse(str);
uri.getQueryParameter(q1);
If you have the url as a String, named str:
String[] part1 = str.split(Pattern.quote("#"));
String[] part2 = part1[1].split(Pattern.quote(","));
double lat = Double.parseDouble(part2[0]);
double lon = Double.parseDouble(part2[1]);

How to get the latitude and longitude from city name in android?

In my application am setting a default value to latitude and longitude.
When i type the cityname in edittext i need to move that point to that place where i need
Use the below code to get the lat and long of city name
String location=cityName;
String inputLine = "";
String result = ""
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
String lat = result.substring(6, result.lastIndexOf(","));
String longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
My suggestion is to use Google GeoCoding REST API , not Geocoder.
Because I once used it in my country,China,I have a error below:
"java.io.IOException: Service not Available"
after search , It`s seems that
"The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists."
So I turn to Google GeoCoding REST API , It`s easy , just request like this:
http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
You will get a json that has geo location in it.
And You can also get Address from locaiton through it:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
If you can read Chinese , I have a blog about this,check this out:
http://wangchao.de/android%E5%88%A9%E7%94%A8google-geocoding-api%E6%9B%BF%E4%BB%A3geocoder-%E8%A7%A3%E5%86%B3%E5%9C%B0%E5%9D%80%E8%AF%AD%E8%A8%80%E9%97%AE%E9%A2%98
Use the Geocoder class.
Geocode gc = new Geocoder();
List<Address> ads = gc.getFromLocationName(cityName,maxResults);
From the docs :
A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
Try to get the GeoPoint of cityname
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(cityname.getText()) * 1E6));
Then u can able to get the latitude and longitude from GeoPoint by using below code.
Double.toString((double)startGP.getLatitudeE6() / 1E6),
Double.toString((double)dest.getLongitudeE6() / 1E6)

Categories

Resources