I’m a noob trying to develop a location based app with google places. I can add my current location the ovelay but i can’t figure out what i am doing wrong so I can add places api results to my overlay. My query is successful in my webbrowser, but not my app i get reQuest Deined . If anyone could help it would be greatly appreciated. Thank You in advance!
Main Activity:
package com.example.fdu;
public class FindDealer extends MapActivity implements LocationListener {
MapView finddealer;
long start;
long stop;
MyLocationOverlay compass;
MapController controller;
int x, y;
GeoPoint touchedPoint;
Drawable delta, zulu;
List<Overlay> overlayList;
LocationManager lm;
String towers;
int lat;
int lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.finddealer);
finddealer = (MapView) findViewById(R.id.mvFindDealer);
finddealer.setBuiltInZoomControls(true);
Touchy tango = new Touchy();
overlayList = finddealer.getOverlays();
overlayList.add(tango);
compass = new MyLocationOverlay(FindDealer.this, finddealer);
overlayList.add(compass);
controller = finddealer.getController();
GeoPoint point = new GeoPoint(51643234, 7848593);//<--Germany
controller.setZoom(12);
finddealer.invalidate();//<--New
delta = getResources().getDrawable(R.drawable.map_pin_48);
zulu= getResources().getDrawable(R.drawable.ic_launcher);
//placing pinpoint at location
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = lm.getBestProvider(crit, false);
Location location = lm.getLastKnownLocation(towers);
if(location != null){
lat = (int) (location.getLatitude()*1E6);
lon = (int) (location.getLongitude()*1E6);
GeoPoint ourLocation = new GeoPoint(lat, lon);
controller.animateTo(ourLocation);
OverlayItem oscar = new OverlayItem(ourLocation, "What's Up", "Homie");
CustomPinPoint custom = new CustomPinPoint(delta, FindDealer.this);
custom.InsertPinpoint(oscar);
overlayList.add(custom);
String googlelat = String.valueOf((int) (location.getLatitude()*1E6));
String googlelon = String.valueOf((int) (location.getLongitude()*1E6));
String googleplacesapi = "https://maps.googleapis.com/maps/api/place/search/json?location=" + googlelat + "," + googlelon + "&radius=50000&type=store&keyword=gold&sensor=false&key=My Key Redacted";
try {
JSONObject googleObject = StoresNearMe.getJSONfromURL(googleplacesapi);
JSONArray parsedGoogle = googleObject.getJSONArray("results");
if(parsedGoogle.equals(null) ){
Toast.makeText(FindDealer.this, "Unable to locate tower", Toast.LENGTH_SHORT).show();
}else{
for (int i = 0; i < parsedGoogle.length(); i++) {
JSONObject parsedlocales = parsedGoogle.getJSONObject(i);
if (i == 0){
JSONObject geometrylocation = parsedlocales.getJSONObject("geometry").getJSONObject("location");
String lat0 = geometrylocation.getString("lat");
String lng0 = geometrylocation.getString("lng");
double overlaylat0 = Double.parseDouble(lat0);
double overlaylng0 = Double.parseDouble(lng0);
GeoPoint resultLocation0 = new GeoPoint((int) (overlaylat0), (int) (overlaylng0));
OverlayItem whiskey = new OverlayItem(resultLocation0, "What's Up", "Homie");
CustomPinPoint custom0 = new CustomPinPoint(zulu, FindDealer.this);
custom0.InsertPinpoint(whiskey);
overlayList.add(custom0);
break;
}
if (i == 1){
JSONObject geometrylocation = parsedlocales.getJSONObject("geometry").getJSONObject("location");
String lat1 = geometrylocation.getString("lat");
String lng1 = geometrylocation.getString("lng");
double overlaylat1 = Double.parseDouble(lat1);
double overlaylng1 = Double.parseDouble(lng1);
GeoPoint resultLocation1 = new GeoPoint((int) (overlaylat1),(int) (overlaylng1));
OverlayItem foxtrot = new OverlayItem(resultLocation1, "What's Up", "Homie");
CustomPinPoint custom1 = new CustomPinPoint(zulu, FindDealer.this);
custom1.InsertPinpoint(foxtrot);
overlayList.add(custom1);
break;
}
if (i == 2){
JSONObject geometrylocation = parsedlocales.getJSONObject("geometry").getJSONObject("location");
String lat2 = geometrylocation.getString("lat");
String lng2 = geometrylocation.getString("lng");
double overlaylat2 = Double.parseDouble(lat2);
double overlaylng2 = Double.parseDouble(lng2);
GeoPoint resultLocation2 = new GeoPoint((int) (overlaylat2),(int) (overlaylng2));
OverlayItem hotel = new OverlayItem(resultLocation2, "What's Up", "Homie");
CustomPinPoint custom2 = new CustomPinPoint(zulu, FindDealer.this);
custom2.InsertPinpoint(hotel);
overlayList.add(custom2);
break;
}
}
}
} catch (JSONException e) {
// Log.d("log_tag","JSON parsing error - Google Places Api:" + e.getMessage());
}
}else{
Toast.makeText(FindDealer.this, "Couldn't get provider", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
lm.removeUpdates(this);
finish();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onResume();
lm.requestLocationUpdates(towers, 500, 1, this);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class Touchy extends Overlay{
public boolean onTouchEvent(MotionEvent echo, MapView mike){
if(echo.getAction()==MotionEvent.ACTION_DOWN){
start=echo.getEventTime();
x = (int) echo.getX();
y = (int) echo.getY();
touchedPoint = finddealer.getProjection().fromPixels(x, y);
}
if(echo.getAction()==MotionEvent.ACTION_UP){
stop=echo.getEventTime();
}
if (stop - start > 1500) {
AlertDialog alert = new AlertDialog.Builder(FindDealer.this).create();
alert.setTitle("Pick an Option");
alert.setMessage("Hag");
alert.setButton("place a pinpoint", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
OverlayItem oscar = new OverlayItem(touchedPoint, "What's Up", "Homie");
CustomPinPoint custom = new CustomPinPoint(zulu, FindDealer.this);
custom.InsertPinpoint(oscar);
overlayList.add(custom);
}
});
alert.setButton2("Get Address", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() / 1E6 , touchedPoint.getLongitudeE6() / 1E6, 1);
if (address.size() > 0){
String display = "";
for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++ ){
display += address.get(0).getAddressLine(i) + "\n";
}
Toast yankee = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
yankee.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
});
alert.setButton3("Toggle View", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (finddealer.isSatellite()){
finddealer.setSatellite(false);
finddealer.setStreetView(true);
}else{
finddealer.setSatellite(true);
finddealer.setStreetView(false);
}
}
});
alert.show();
return true;
}
return false;
}
}
@Override
public void onLocationChanged(Location l) {
// TODO Auto-generated method stub
lat = (int) (l.getLatitude()* 1E6);
lon = (int) (l.getLongitude()* 1E6);
GeoPoint ourLocation = new GeoPoint(lat, lon);
OverlayItem oscar = new OverlayItem(ourLocation, "What's Up", "Homie");
CustomPinPoint custom = new CustomPinPoint(delta, FindDealer.this);
custom.InsertPinpoint(oscar);
overlayList.add(custom);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();//<--New
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT ).show();//<--New
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(FindDealer.this, "StatusChanged" +provider, Toast.LENGTH_SHORT).show();
}
}
Stores Near Me
public class StoresNearMe {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject googleObject = 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{
googleObject = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return googleObject;
}
}
CustomPinPoint Java
public class CustomPinPoint extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context charlie;
public CustomPinPoint(Drawable defaultMarker) {
super(boundCenter(defaultMarker));
// TODO Auto-generated constructor stub
}
public CustomPinPoint(Drawable mike, Context context) {
// TODO Auto-generated constructor stub
this(mike);
charlie = context;
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return pinpoints.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return pinpoints.size();
}
public void InsertPinpoint(OverlayItem india){
pinpoints.add(india);
this.populate();
}
}
In your
CustomPinPointconstructor you wroteyou’re calling
this(mike)instead ofsuper(mike)So you basically never calls the super constructor which won’t create the ItemizedOverlay
Also, I don’t know if it matters, but comparing this tutorial http://about-android.blogspot.co.il/2010/03/steps-to-place-marker-in-map-overlay.html to your code,
should be
one last thing, you’re calling
this.populate()in yourpublic void InsertPinpoint(OverlayItem india){
since you haven’t override it, it should be only
populateto make sure you call super’s populate(). Don’t know if it makes a different though.EDIT:
Well, after some playing with your code, I found a few things..
First, your query returns no results. when I removed the
keyword=goldfrom the URL, it started to give results back.I’m a bit confused with your coordinates, GeoPoint to Location and stuff, The first result, using your code was placed in the sea near Africa. I assume that’s not where your store is. however, that coordinates for it were 81,74 or something like that. hardly an exact coordinate. Maybe you should try multiplying it by 1E6 like you did before the for loop.
But regarding your original question.
After making the changes I told you before, it doesn’t work. but reverting back to your original code, it does. The problem was that you simply wasn’t getting any results with your original URL. once I changed the URL, it receives the places and draws the marker (though you need to tinker a bit with displaying it correctly)
Hope that works for you too