android GoogleMap定位(四)

根据输入的名称查询

 

注:地点输入错误的时候会导致程序崩溃,暂没做处理。(可进行提示的)

 

主界面:

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);
		// 显示放大缩小控制按钮
		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, "输入有效地址",
							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));
	}
}

处理请求(推荐此处用service,handle来进行处理交互)

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.execute(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;
}
}
 

 效果图


android GoogleMap定位(四)

源码

你可能感兴趣的:(android,googlemap)