android网络-用sax对googlemap的xml进行解析,获取经纬度

用sax解析器对googlemap的xml进行解析(为做地图而准备大笑)

解析部分:只要进入location的lat和lng提取出经纬度信息就大功告成了

程序效果:按下按钮,获取输入地点的经纬度信息并显示


xml文件举例

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>sublocality</type>
  <type>political</type>
  <formatted_address>中国天津市河西区</formatted_address>
  <address_component>
   <long_name>河西区</long_name>
   <short_name>河西区</short_name>
   <type>sublocality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>天津</long_name>
   <short_name>天津</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>天津市</long_name>
   <short_name>天津市</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>中国</long_name>
   <short_name>CN</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>39.1096630</lat>
    <lng>117.2235640</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>39.0750228</lat>
     <lng>117.1595343</lng>
    </southwest>
    <northeast>
     <lat>39.1442862</lat>
     <lng>117.2875937</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>39.0499627</lat>
     <lng>117.1790660</lng>
    </southwest>
    <northeast>
     <lat>39.1174847</lat>
     <lng>117.2924505</lng>
    </northeast>
   </bounds>
  </geometry>
 </result>
</GeocodeResponse>


主activity

package com.song;
//用sax解析器对google——map的xml进行解析
//解析出经纬度  
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.Header;
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.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class C614_Googlemap3Activity extends Activity {
    /** Called when the activity is first created. */
	Button button;
	TextView textview;
	EditText edittext;
	MyHandler handler; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button);
        textview=(TextView)findViewById(R.id.textview);
        edittext=(EditText)findViewById(R.id.edittext);  
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HttpClient client=new DefaultHttpClient();
		        HttpGet get=new HttpGet("http://maps.googleapis.com/maps/api/geocode/xml?address="+edittext.getText().toString()+"&sensor=false");
		        try {
					HttpResponse response=client.execute(get);//client执行请求
					Header[] headers=response.getHeaders("Content-Type");
					System.out.println(headers[0].getValue()); //输出编码格式 
					HttpEntity entity=response.getEntity();//内容
					InputStream input=entity.getContent();//获得含有中文网址的输入流
					
					SAXParserFactory factory=SAXParserFactory.newInstance();
					SAXParser parser=factory.newSAXParser();//新建解析器
					XMLReader reader=parser.getXMLReader();
					handler=new MyHandler();
					reader.setContentHandler(handler);
					//执行解析input以utf-8编码
					reader.parse(new InputSource(new InputStreamReader(input,"UTF-8")));
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				textview.setText(handler.getData());
			}
		});
        

    }
}

解析类MyHandler

package com.song;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class MyHandler implements ContentHandler{
     
	boolean flag_lat,flag_lng,flag_location;
	String str_lat,str_lng;
	public String getData()
	{
		return "经度"+str_lng+"\n"+"纬度"+str_lat;
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		if(flag_lat==true)
		{
			str_lat=new String(ch,start,length);
		}
		if(flag_lng==true)
		{
			str_lng=new String(ch,start,length);
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		flag_lat=false;
		flag_lng=false;
		if(localName.equals("location"))
		{
			flag_location=false;
		}
		
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("location"))
		{
			flag_location=true;
		}
		if(flag_location==true)
		{
			if(localName.equals("lat"))
			{
				flag_lat=true;
				
			}
			if(localName.equals("lng"))
			{
				flag_lng=true;
			}
		}
		
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

}

显示效果






你可能感兴趣的:(android网络-用sax对googlemap的xml进行解析,获取经纬度)