google提供的Geocoding服务

1:我们相拥Google提供的Geocoding的定位服务,主要有二种方式:

(1)使用Android内置的GeoCoder类 的getFromLocation()和getLocationFromName();

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
   <Button 
        android:id="@+id/requireaddressButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="根据经纬度获取用户地址"
        />
        <Button 
        android:id="@+id/requireabsolutaddressButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="根据地点名称获取经纬度"
        />
</LinearLayout>


---------------------------------------------------------------------------------------------------------‘

package com.example.userlocation;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*
 * 这个Activity的主要功能就是使用Android内置的Gencoder类 去访问google内置的gencoding服务
 * 根据用户提供的具体地址查找经纬度
 * 根据用户的经纬度找到具体的地址
 * 
 * 但是据说Gencoder这个服务不好使,所以我们可以通过访问具体的http:网址进行访问
 * */
public class GeocodingActivity extends Activity {
//使用goole 的geocoding的服务,可以Android内置了Geocoder类,来访问这个服务
private Button geocoderButton;
private Button reversegeocoderButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geocoding);

geocoderButton=(Button)findViewById(R.id.requireaddressButton);
reversegeocoderButton=(Button)findViewById(R.id.requireabsolutaddressButton);
geocoderButton.setOnClickListener(new GeocoderButtonListener());
reversegeocoderButton.setOnClickListener(new ReverseGeocoderButtonListener());

}
/*
* 根据用户提供的名字找到经纬度

* */
class GeocoderButtonListener implements OnClickListener{


@Override
public void onClick(View v) {
new GeocoderTask().execute();
}

}

/*
* 这是一个异步的任务,减少时间的消耗
* */
class GeocoderTask extends AsyncTask<Integer,Integer,Integer>{


@Override
protected Integer doInBackground(Integer... params) {
try {
//获取Geocoder对象
Geocoder geocoder=new Geocoder(GeocodingActivity.this);
//根据具体地址的名字获取经纬度
List<Address> address=geocoder.getFromLocationName("China", 1);
System.out.println(address.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
/*
* 这是一个异步的任务,减少时间的消耗
* */
class ReverseGeocoderTask extends AsyncTask<Integer,Integer,Integer>{


@Override
protected Integer doInBackground(Integer... params) {
try {
//获取Geocoder对象
Geocoder geocoder=new Geocoder(GeocodingActivity.this);
//根据经纬度获取具体的地址
List<Address> address=geocoder.getFromLocation(10.101,20.201,1);
System.out.println(address.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

/*
* 根据纬度找到地址

* */
class ReverseGeocoderButtonListener implements OnClickListener{


@Override
public void onClick(View v) {
new ReverseGeocoderTask().execute();
}

}
}

(2)第二种方式就是我们自己访问Google的解析精度和纬度的url。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 
         android:id="@+id/showAddress"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="查询自己所在的地址"
        />
</LinearLayout>

package com.example.userlocation;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import junit.framework.TestResult;
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 com.google.gson.Gson;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;




/*
 * 我们也可以使用HttpClient访问指定的url网址,从而得到相应的返回数据,是json格式,
 * 我们在使用Gson对其进行解析,因为我们可以得到对象
 * */
public class VisitGencodingByHttp extends Activity {

private Button showAddressButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visitgencodingbyhttp);
showAddressButton=(Button)findViewById(R.id.showAddress);
showAddressButton.setOnClickListener(new ShowAddressButton() );
}

class ShowAddressButton implements OnClickListener{


@Override
public void onClick(View view) {
//要访问的具体转换的地址
String url="http://maps.googleapis.com/maps/api/geocode/json?latlng=10.1,20.3&sensor=false";
//创建一个HttpClient 自动向服务器发送请求
HttpClient httpClient=new DefaultHttpClient();
//服务器返回的数据
String responseData="";
HttpResponse response;
HttpEntity entity;
BufferedReader br;
try {
response = httpClient.execute(new HttpGet(url));
entity=response.getEntity();
br=new BufferedReader(new InputStreamReader(entity.getContent()));
String line=null;
while((line=br.readLine())!=null){
responseData=responseData+line;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//将服务器返回的json格式的数据转换成对象
Gson gson=new Gson();

TestResult testResult=gson.fromJson(responseData, TestResult.class);
System.out.println(testResult);

}

}
}

package po;


import java.util.List;


public class TestResult {


private String status;
private List<Result> results;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}


package po;


public class Result {


private String[] types;
private String formateed_address;
public String[] getTypes() {
return types;
}
public void setTypes(String[] types) {
this.types = types;
}
public String getFormateed_address() {
return formateed_address;
}
public void setFormateed_address(String formateed_address) {
this.formateed_address = formateed_address;
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值