Query by input name
Note: If the location is entered incorrectly, the program crashes and no processing is performed. (Prompts are available)
Main Interface:
Java code
Package com. hc;
Import java. util. List;
Import com. google. android. maps. GeoPoint;
Import com. google. android. maps. MapActivity;
Import com. google. android. maps. MapController;
Import com. google. android. maps. MapView;
Import com. google. android. maps. Overlay;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. RadioGroup;
Import android. widget. RadioGroup. OnCheckedChangeListener;
Import android. widget. Toast;
Public class AddressActivity extends MapActivity {
/** Called when the activity is first created .*/
Button locBn;
MapView mv;
EditText address_value;
MapController controller;
Bitmap posBitmap;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. address );
PosBitmap = BitmapFactory. decodeResource (getResources (),
R. drawable. icon );
Mv = (MapView) findViewById (R. id. mv );
Address_value = (EditText) findViewById (R. id. address_value );
// Display the zoom in/out control button
Mv. setBuiltInZoomControls (true );
Controller = mv. getController ();
LocBn = (Button) findViewById (R. id. location );
LocBn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
String address = address_value.getEditableText (). toString (). trim ();
If (address. equals ("")){
Toast. makeText (AddressActivity. this, "enter a valid address ",
Toast. LENGTH_LONG). show ();
} Else {
Double [] result = ConvertUtil. getLocationInfo (address );
UpdateMapView (result [0], result [1]);
}
}
});
LocBn. performClick ();
}
@ Override
Protected boolean isRouteDisplayed (){
// TODO Auto-generated method stub
Return true;
}
Private void UpdateMapView (double dlong, double dLat ){
GeoPoint gp = new GeoPoint (int) (dLat * 1E6), (int) (dlong * 1E6 ));
Mv. displayZoomControls (true );
Controller. animateTo (gp );
List <Overlay> ol = mv. getOverlays ();
Ol. clear ();
Ol. add (new PosOverLay (gp, posBitmap ));
}
}
Request Processing (service and handle are recommended for interaction processing)
Package com. hc;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. client. ClientProtocolException;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. methods. HttpGet;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. json. JSONException;
Import org. json. JSONObject;
Public class ConvertUtil {
Public static double [] getLocationInfo (String address ){
HttpClient client = new DefaultHttpClient ();
HttpGet httpGet = new HttpGet ("http://maps.google.com/maps/api/geocode/json? Address = "+ address +" ka & sensor = false ");
StringBuffer sb = new StringBuffer ();
HttpResponse response;
Try {
Response = client.exe cute (httpGet );
HttpEntity entity = response. getEntity ();
InputStream in = entity. getContent ();
BufferedReader reader = new BufferedReader (new InputStreamReader (in, "UTF-8 "));
// Int len = 0;
// While (len = in. read ())! =-1 ){
// Sb. append (char) len );
//}
String line = null;
While (line = reader. readLine ())! = Null ){
Sb. append (line );
}
In. close ();
Reader. close ();
JSONObject json = new JSONObject (sb. toString ());
JSONObject location = json. getJSONArray ("results"). getJSONObject (0). getJSONObject ("geometry"). getJSONObject ("location ");
Double x = location. getDouble ("lng ");
Double y = location. getDouble ("lat ");
Return new double [] {x, y };
} Catch (ClientProtocolException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (JSONException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return null;
}
}