Hi,This is simple source code to find out the location without using geocoading in android.When user want to find out any location in google map,use this source code to make helpful for urself.
Here,I am making two class but you can do it only single one.
<b>mapfinder.class</b>
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.http.HttpEntity;
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;
public class mapfinder extends MapActivity {
private MapView map=null;
Vector v;
private MyLocationOverlay me=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapdemo);
//locationlocator
final String httpPath = "http://maps.google.com/maps/geo?q="+locationlocator.addressInput.getText().toString()+"&output=csv";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(httpPath);
try {
HttpEntity httpEntity = httpclient.execute(httpget).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ss = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
ss=line;
}
v = new Vector();
StringTokenizer st = new StringTokenizer (ss,",");
while(st.hasMoreTokens()){
//Log.d("cccccccccccccc","ccccccccccccccccc"+st.nextToken());
v.add(st.nextToken());
}
// for(int i=0;i<ss.length();i++){
// Log.d("cccccccccccccc","ccccccccccccccccc"+ss.indexOf(i));
// }
inputStream.close();
// text.setText(stringBuilder.toString());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// Toast.makeText(AndroidHttpGetActivityActivity.this, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// Toast.makeText(AndroidHttpGetActivityActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
Log.d("vvvvvvvvvvvvvvvvvvvvvvvvvvv","vvvvvvvvvvvvvvvvvvvvvvv"+v.elementAt(3).toString());
map=(MapView)findViewById(R.id.map);
map.getController().setCenter(getPoint
(Double.parseDouble(v.elementAt(2).toString()),Double.parseDouble(v.elementAt(2).toString())));
map.getController().animateTo(getPoint
(Double.parseDouble(v.elementAt(2).toString()),Double.parseDouble(v.elementAt(3).toString())));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return(false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
items.add(new OverlayItem(getPoint
(Double.parseDouble(v.elementAt(2).toString()),Double.parseDouble(v.elementAt(3).toString())),
"UN", "United Nations"));
// items.add(new OverlayItem(getPoint
// (30.7313450,76.7753850),
// "Lincoln Center",
// "Home of Jazz at Lincoln Center"));
// items.add(new OverlayItem(getPoint
// (40.765136435316755,-173.97989511489868),
// "Carnegie Hall",
// "Where you go with practice, practice, practice"));
// items.add(new OverlayItem(getPoint
// (40.70686417491799,-174.01572942733765),
// "The Downtown Club",
// "Original home of the Heisman Trophy"));
populate();
}
@Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(mapfinder.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
@Override
public int size() {
return(items.size());
}
}
}
locationlocator.class: This is second class
import java.util.Vector;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class locationlocator extends Activity implements OnClickListener {
// @Override
// protected boolean isRouteDisplayed() {
// return false;
// }
final String httpPath = "addressInput";
private MapView map;
private Button btnSearch;
public static EditText addressInput;
private ProgressDialog pd;
private Vector<String> v1;
private MyLocationOverlay me=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// map = (MapView) findViewById(R.id.mapview); // Get map from XML
btnSearch = (Button) findViewById(R.id.simplebtn_search); // Get button from xml
addressInput = (EditText) findViewById(R.id.simpleadress);
btnSearch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
startActivity( new Intent(locationlocator.this,mapfinder.class));
}
}
After making this class. u can also add XML file for both of class.Also remember to add permission in manifest file.
May b this code helpful for you.
No comments:
Post a Comment