Note that there are some explanatory texts on larger screens.

plurals
  1. POGeocode results based on current location
    primarykey
    data
    text
    <p>In one of my activities i need to be able to do a geocoding (find a location by address String search). The problem is that my results are much too broad. When i search for "mcdonalds" i get results that are in different parts of the USA. How can i make it so a user can search for nearby restaurants (or any location) and the results will be within a certain distance? I basically need more precise results for my application. Here is a screenshot of whats happening: <img src="https://i.stack.imgur.com/T2naX.png" alt="enter image description here"></p> <pre><code>public class MainActivity extends MapActivity { HelloItemizedOverlay itemizedOverlay; List&lt;Overlay&gt; mapOverlays; Drawable drawable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapView myMap = (MapView) findViewById(R.id.mapview); myMap.setBuiltInZoomControls(true); MapController mc = myMap.getController(); mapOverlays = myMap.getOverlays(); drawable = this.getResources().getDrawable(R.drawable.androidmarker); itemizedOverlay = new HelloItemizedOverlay(drawable, this); //geopoints are cordinates in microdegrees or degrees * E6 GeoPoint point = new GeoPoint(34730300, -86586100); //GeoPoint point2 = locatePlace("texas", mc); //HelloItemizedOverlay.locatePlace("Texas", mc, myMap); //overlayitems are items that show the point of location to the user OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "im in huntsville"); //OverlayItem overlayitem2 = new OverlayItem(point2, "Texas", "hi"); //itemizedoverlay is used here to add a drawable to each of the points itemizedOverlay.addOverlay(overlayitem); //itemizedOverlay.addOverlay(overlayitem2); //this adds the drawable to the map //this method converts the search address to locations on the map and then finds however many you wish to see. locatePlace("mcdonalds", mc, 5); mapOverlays.add(itemizedOverlay); //this animates to the point desired (i plan on having "point" = current location of the user) mc.animateTo(point); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected boolean isRouteDisplayed(){ return false; } public void locatePlace(String locName, MapController mc, int numberToDisplay) { // code to make the google search via string work // i use the Geocoder class is used to handle geocoding and reverse-geocoding. So make an instance of this class to work with the methods included Geocoder geoCoder1 = new Geocoder(this, Locale.getDefault()); try { List&lt;Address&gt; searchAddresses = geoCoder1.getFromLocationName(locName, numberToDisplay); // gets a max of 5 locations if (searchAddresses.size() &gt; 0) { //iterate through using an iterator loop (for loop would have been fine too) //Iterator&lt;Address&gt; iterator1 = searchAddresses.iterator(); for (int i=0; i &lt; searchAddresses.size(); i++){ //while (iterator1.hasNext()){ //step1 get a geopoint GeoPoint tempGeoP = new GeoPoint( (int) (searchAddresses.get(i).getLatitude()*1E6), (int) (searchAddresses.get(i).getLongitude()*1E6) ); //step2 add the geopoint to the Overlay item OverlayItem tempOverlayItm = new OverlayItem(tempGeoP, locName, "this is " + locName); //step3 add the overlay item to the itemized overlay HelloItemizedOverlay tempItemizedOverlay = new HelloItemizedOverlay(drawable, this); // its breakking here......... tempItemizedOverlay.addOverlay(tempOverlayItm); //the itemized overlay is added to the map Overlay mapOverlays.add(tempItemizedOverlay); } } } catch (IOException e) { e.printStackTrace(); // Log.e("the error", "something went wrong: "+ e); }//finally {} } } </code></pre> <p>// here is the important code from the Itemized overlay class</p> <pre><code>public HelloItemizedOverlay(Drawable defaultMarker, Context context) { super(boundCenter(defaultMarker)); myContext = context; } public void addOverlay(OverlayItem overlay){ mOverlays.add(overlay); populate(); } </code></pre> <p>Thanks, Adam</p> <p>Here is some code after adjusting to Vishwa's comments:</p> <pre><code> // these 2 variables are my current location latitudeCurrent = 34730300; // make this dynamic later longitudeCurrent = -86586100; // make this dynamic later LLlatitude = (latitudeCurrent - 100000)/(1E6); //lowerleft latitude = original lat - (1)*degree/10 LLlongitude = (longitudeCurrent+ 100000)/(1E6);//lowerleft longitude = original longitude - (1)*degree/10 URlatitude = (latitudeCurrent + 100000)/(1E6); //upperright latitude = original + (1)*degree/10 URlongitude = (longitudeCurrent+ 100000)/(1E6); //upperright longitude = original longitude + (1)*degree/10 try { List&lt;Address&gt; searchAddresses = geoCoder1.getFromLocationName(locName, numberToDisplay, LLlatitude, LLlongitude, URlatitude, URlongitude); </code></pre> <p><img src="https://i.stack.imgur.com/jJc7p.png" alt="after creating a square of allowable results from the getfromlocationname()"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload