您现在的位置是:主页 > news > 托管平台/郑州seo关键词

托管平台/郑州seo关键词

admin2025/4/26 2:29:41news

简介托管平台,郑州seo关键词,兰州做网站客户,怎么做谷歌这样的网站2019独角兽企业重金招聘Python工程师标准>>> android.location 包提供了一些工具来实现基于位置的服务。主要包括 Geocoder 类和LocationManager服务。首先介绍 Geocoder。 1.使用Android进行地理编码 如果打算使用地图做一些实际的事情,可能必须将地址(…

托管平台,郑州seo关键词,兰州做网站客户,怎么做谷歌这样的网站2019独角兽企业重金招聘Python工程师标准>>> android.location 包提供了一些工具来实现基于位置的服务。主要包括 Geocoder 类和LocationManager服务。首先介绍 Geocoder。 1.使用Android进行地理编码 如果打算使用地图做一些实际的事情,可能必须将地址(…

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

 android.location 包提供了一些工具来实现基于位置的服务。主要包括 Geocoder 类和LocationManager服务。首先介绍 Geocoder。

1.使用Android进行地理编码

     如果打算使用地图做一些实际的事情,可能必须将地址(或位置)转换为纬度/经度对。此概念称为地理编码,android.location.Geocoder 类提供了此功能。实际上,Geocoder既提供了前向转换,也提供了后向转换--------它可以获取地址并返回经度/纬度,也可以将经度/纬度对转换为一组地址。该类提供了以下方法。
Java代码 复制代码
  1. List<Address>  getFromLocation(double latitude,double longitude,int maxResults);   
  2. List<Address> getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitue, double upperRightLatitude, double upperRightLongitude);   
  3. List<Address>  getFromLocationName(String locationName, int maxResults)。  
List<Address>  getFromLocation(double latitude,double longitude,int maxResults);
List<Address> getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitue, double upperRightLatitude, double upperRightLongitude);
List<Address>  getFromLocationName(String locationName, int maxResults)。

      事实证明,计算地址并不完全属于科学范畴,因为可以通过各种方式来描述位置。例如,getFromLocationName() 方法可以获得地方的名称、物理地址和机场编号,或者该位置的流行名称。因此,这些方法提供了一个地址列表,而不是一个地址。因为这些方法返回一个列表,所以最好提供1~5的maxResults 值来限制结果集,下面我们来看一个查询地址的例子,和原来一样我们得自己定义一个类来继承MapActivity,先来看看运行效果。

c306d0e8-47a1-372d-bea2-20a01b74aa1e.jpg

  我们的布局文件
Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout android:layout_width="fill_parent"  
  8.         android:layout_alignParentBottom="true"  
  9.         android:layout_height="wrap_content" android:orientation="vertical">  
  10.         <EditText  
  11.             android:id="@+id/location"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="请输入地址..."/>  
  15.         <Button  
  16.             android:id="@+id/geocodeBtn"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="Find Location" />  
  20.     </LinearLayout>  
  21.   
  22.            
  23.         <com.google.android.maps.MapView  
  24.             android:id="@+id/geoMap"  
  25.             android:clickable="true"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="320px"  
  28.             android:apiKey="0XemFEdFemEDqY3dE3Ko9ELJX12MRLjJGKEJ01g"  
  29.                  />  
  30.                
  31. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:layout_width="fill_parent"android:layout_alignParentBottom="true"android:layout_height="wrap_content" android:orientation="vertical"><EditTextandroid:id="@+id/location"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="请输入地址..."/><Buttonandroid:id="@+id/geocodeBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Find Location" /></LinearLayout><com.google.android.maps.MapViewandroid:id="@+id/geoMap"android:clickable="true"android:layout_width="fill_parent"android:layout_height="320px"android:apiKey="0XemFEdFemEDqY3dE3Ko9ELJX12MRLjJGKEJ01g"/></RelativeLayout>


  AndroidManifest.xml文件
Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="xiaohang.zhimeng" android:versionCode="1" android:versionName="1.0">  
  4.     <uses-sdk android:minSdkVersion="10" />  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <uses-library android:name="com.google.android.maps" />  
  7.         <activity android:name=".MainActivity" android:label="@string/app_name">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN" />  
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  
  13.     </application>  
  14.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  15.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  16.     <uses-permission android:name="android.permission.INTERNET" />  
  17. </manifest>  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="xiaohang.zhimeng" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="10" /><application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="com.google.android.maps" /><activity android:name=".MainActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.INTERNET" />
</manifest>


   我们的MainActivity类
Java代码 复制代码
  1. package xiaohang.zhimeng;   
  2.   
  3. import java.util.List;   
  4.   
  5. import com.google.android.maps.GeoPoint;   
  6. import com.google.android.maps.MapActivity;   
  7. import com.google.android.maps.MapView;   
  8. import android.location.Address;   
  9. import android.location.Geocoder;   
  10. import android.os.Bundle;   
  11. import android.view.View;   
  12. import android.view.View.OnClickListener;   
  13. import android.widget.Button;   
  14. import android.widget.EditText;   
  15.   
  16. public class MainActivity extends MapActivity {   
  17.   
  18.     Geocoder geocoder = null;   
  19.     MapView mapView = null;   
  20.   
  21.     @Override  
  22.     protected boolean isLocationDisplayed() {   
  23.         return false;   
  24.     }   
  25.   
  26.     @Override  
  27.     protected boolean isRouteDisplayed() {   
  28.         return false;   
  29.     }   
  30.   
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {   
  33.         super.onCreate(savedInstanceState);   
  34.         setContentView(R.layout.main);   
  35.         mapView = (MapView) findViewById(R.id.geoMap);   
  36.         mapView.setBuiltInZoomControls(true);   
  37.         // 经度:116.3946533203125   
  38.         // 纬度:39.87601941962116   
  39.         int lat = (int) (39.87601941962116 * 1E6);   
  40.         int lng = (int) (116.3946533203125 * 1E6);   
  41.         GeoPoint pt = new GeoPoint(lat, lng);   
  42.         mapView.getController().setZoom(10);   
  43.         mapView.getController().setCenter(pt);   
  44.   
  45.         Button geoBtn = (Button) findViewById(R.id.geocodeBtn);   
  46.   
  47.         geocoder = new Geocoder(this);   
  48.   
  49.         geoBtn.setOnClickListener(new OnClickListener() {   
  50.             @Override  
  51.             public void onClick(View v) {   
  52.                 try {   
  53.                     EditText loc = (EditText) findViewById(R.id.location);   
  54.                     String locationName = loc.getText().toString();   
  55.   
  56.                     List<Address> addressList = geocoder.getFromLocationName(   
  57.                             locationName, 5);   
  58.                     if (addressList != null && addressList.size() > ) {   
  59.                         int lat = (int) (addressList.get().getLatitude() * 1E6);   
  60.                         int lng = (int) (addressList.get().getLongitude() * 1E6);   
  61.   
  62.                         GeoPoint pt = new GeoPoint(lat, lng);   
  63.                         mapView.getController().setZoom(15);   
  64.                         mapView.getController().setCenter(pt);   
  65.                     }   
  66.                 } catch (Exception e) {   
  67.                     e.printStackTrace();   
  68.                 }   
  69.             }   
  70.         });   
  71.     }   
  72. }  
package xiaohang.zhimeng;import java.util.List;import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends MapActivity {Geocoder geocoder = null;MapView mapView = null;@Overrideprotected boolean isLocationDisplayed() {return false;}@Overrideprotected boolean isRouteDisplayed() {return false;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mapView = (MapView) findViewById(R.id.geoMap);mapView.setBuiltInZoomControls(true);// 经度:116.3946533203125// 纬度:39.87601941962116int lat = (int) (39.87601941962116 * 1E6);int lng = (int) (116.3946533203125 * 1E6);GeoPoint pt = new GeoPoint(lat, lng);mapView.getController().setZoom(10);mapView.getController().setCenter(pt);Button geoBtn = (Button) findViewById(R.id.geocodeBtn);geocoder = new Geocoder(this);geoBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {EditText loc = (EditText) findViewById(R.id.location);String locationName = loc.getText().toString();List<Address> addressList = geocoder.getFromLocationName(locationName, 5);if (addressList != null && addressList.size() > 0) {int lat = (int) (addressList.get(0).getLatitude() * 1E6);int lng = (int) (addressList.get(0).getLongitude() * 1E6);GeoPoint pt = new GeoPoint(lat, lng);mapView.getController().setZoom(15);mapView.getController().setCenter(pt);}} catch (Exception e) {e.printStackTrace();}}});}
}


   但是如果这个类要是这么写,就会有问题了。但是感觉这么写是没错误的,但是每当你点击一次Find Location按钮就会出现一次异常(IOException),异常见下图。

7416da98-d708-3f9d-bdff-6173a062e108.jpg

    说什么服务不可以用,去网上一搜 遇见这问题的人还真不少,eoe上边也有 但是没说怎么解决,不了了之了。至于为什么有这个异常,我也不能准确的告诉大家 我也不太清楚,网上说什么的都有。 什么 是bug之类的 等等,大家可以去网上搜搜 最后自己这样写一次试试。但是解决方法还是有的,在这里 http://code.google.com/p/android/issues/detail?id=8816  老外搞出来的方法,他们讨论的也很火热,感觉比我们 积极很多。大家看 21楼就行了, 呵呵。

   具体解决方法就是自己定义了两个静态方法,我把这两个方法放到了MapUtility类中,这个类是我自己定义的。
MapUtility类
Java代码 复制代码
  1. package xiaohang.zhimeng.tool;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import org.apache.http.HttpEntity;   
  6. import org.apache.http.HttpResponse;   
  7. import org.apache.http.client.ClientProtocolException;   
  8. import org.apache.http.client.HttpClient;   
  9. import org.apache.http.client.methods.HttpGet;   
  10. import org.apache.http.impl.client.DefaultHttpClient;   
  11. import org.json.JSONArray;   
  12. import org.json.JSONException;   
  13. import org.json.JSONObject;   
  14.   
  15. import com.google.android.maps.GeoPoint;   
  16.   
  17. public class MapUtility {   
  18.     public static JSONObject getLocationInfo(String address) {   
  19.   
  20.         HttpGet httpGet = new HttpGet("http://maps.google."  
  21.                 + "com/maps/api/geocode/json?address=" + address   
  22.                 + "ka&sensor=false");   
  23.         HttpClient client = new DefaultHttpClient();   
  24.         HttpResponse response;   
  25.         StringBuilder stringBuilder = new StringBuilder();   
  26.   
  27.         try {   
  28.             response = client.execute(httpGet);   
  29.             HttpEntity entity = response.getEntity();   
  30.             InputStream stream = entity.getContent();   
  31.             int b;   
  32.             while ((b = stream.read()) != -1) {   
  33.                 stringBuilder.append((char) b);   
  34.             }   
  35.         } catch (ClientProtocolException e) {   
  36.         } catch (IOException e) {   
  37.         }   
  38.   
  39.         JSONObject jsonObject = new JSONObject();   
  40.         try {   
  41.             jsonObject = new JSONObject(stringBuilder.toString());   
  42.         } catch (JSONException e) {   
  43.             e.printStackTrace();   
  44.         }   
  45.   
  46.         return jsonObject;   
  47.     }   
  48.   
  49.     // After executing this, another method converts that JSONObject into a   
  50.     // GeoPoint.   
  51.   
  52.     public static GeoPoint getGeoPoint(JSONObject jsonObject) {   
  53.   
  54.         Double lon = new Double();   
  55.         Double lat = new Double();   
  56.   
  57.         try {   
  58.   
  59.             lon = ((JSONArray) jsonObject.get("results")).getJSONObject()   
  60.                     .getJSONObject("geometry").getJSONObject("location")   
  61.                     .getDouble("lng");   
  62.   
  63.             lat = ((JSONArray) jsonObject.get("results")).getJSONObject()   
  64.                     .getJSONObject("geometry").getJSONObject("location")   
  65.                     .getDouble("lat");   
  66.   
  67.         } catch (JSONException e) {   
  68.             e.printStackTrace();   
  69.         }   
  70.         return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));   
  71.     }   
  72.   
  73. }  
package xiaohang.zhimeng.tool;import java.io.IOException;
import java.io.InputStream;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import com.google.android.maps.GeoPoint;public class MapUtility {public static JSONObject getLocationInfo(String address) {HttpGet httpGet = new HttpGet("http://maps.google."+ "com/maps/api/geocode/json?address=" + address+ "ka&sensor=false");HttpClient client = new DefaultHttpClient();HttpResponse response;StringBuilder stringBuilder = new StringBuilder();try {response = client.execute(httpGet);HttpEntity entity = response.getEntity();InputStream stream = entity.getContent();int b;while ((b = stream.read()) != -1) {stringBuilder.append((char) b);}} catch (ClientProtocolException e) {} catch (IOException e) {}JSONObject jsonObject = new JSONObject();try {jsonObject = new JSONObject(stringBuilder.toString());} catch (JSONException e) {e.printStackTrace();}return jsonObject;}// After executing this, another method converts that JSONObject into a// GeoPoint.public static GeoPoint getGeoPoint(JSONObject jsonObject) {Double lon = new Double(0);Double lat = new Double(0);try {lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat");} catch (JSONException e) {e.printStackTrace();}return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));}}


   这两个方法就不多解释了,反正我们最终的目的是需要一个GeoPoint对象,它给我们返回正确的GeoPoint对象就行了,大家可以去原地址去看看他们怎么讨论了,链接在上边我已经贴出来了,现在我们在搜索tian an men 就会把tian an men给我们显示在地图的中央了,没修改之前是 一点 一次异常,现在的效果如下图,( 大家知道 tian an men是啥意思吧,自动eye被 oracle搞过一次 法律意识越来越强啊。。。。  icon_evil.gif )

db6a58c9-0552-3651-9d2f-e4c7932c414a.jpg

     要体验地理编码在Android中的使用,可以在 EditText字段中键入位置名称或它的地址,然后点击 Find Location按钮。要找到某个位置的地址,调用 Geocoder的 getFromLocationName()方法。位置可以是地址或流行名称,比如 “故宫”。地理编码是一项实时操作,所以根据Android文档的建议,我们建议将结果限制为5个。对getFromLocationName()的调用返回一个地址列表。示例程序获取该地址列表并处理第一个地址(如果存在)。每个地址都具有经纬度。可以使用它来创建 GeoPoint。然后调用地图控制器并导航到该点。缩放级别可以设置为1~21 (包括1和21)的整数。在从1向21移动时,缩放级别每次将增加两级。

      对于地理编码,应该了解几点。第一,返回的地址并不总是准确的地址。显然,由于返回的地址列表取决于输入的准确度,所以需要尽量向 Geocoder 提供准确的位置名称。第二,尽量将 maxResults 设置为1~5的值。最后,应该认真考虑在不同于UI线程的线程中执行地理编码操作。这有两个原因。第一个很明显:这项操作很耗时,而且你一定不希望UI在进行地理编码时停顿,如果停顿会阻塞整个用户界面。当在执行一些耗时的操作的时候,不能及时地分发 事件,包括用户界面重绘事件。从用户的角度来看,应用程序看上去像挂掉了。更糟糕的是,如果阻塞应用程序的时间过长(5秒钟)Android会向用户提示 一些信息,即打开一个“应用程序没有相应(application not responding)”ANR 的对话框,关于线程的更多内容请大家看这里 http://byandby.iteye.com/blog/825071  第二个原因是,对于移动设备,始终需要假设网络连接可能丢失并且连接很弱。因此,需要恰当地处理I/O 异常和超时。计算出地址以后,就可以将结果发送给UI线程,下面我们看看使用后台线程进行地理编码,这个例子和上一个例子一样 唯一不同的就是 这个例子把查询地址的操作放到 另外一个辅助线程里来执行了,上一个是所有的操作都在UI线程里,下面是我们的Activity类,GeocodingDemoActivity,我们看看怎么实现。。。

  GeocodingDemoActivity类
Java代码 复制代码
  1. package xiaohang.zhimeng;   
  2.   
  3. import java.util.List;   
  4. import org.json.JSONObject;   
  5. import xiaohang.zhimeng.tools.MapUtility;   
  6. import android.app.AlertDialog;   
  7. import android.app.Dialog;   
  8. import android.app.ProgressDialog;   
  9. import android.location.Address;   
  10. import android.location.Geocoder;   
  11. import android.os.Bundle;   
  12. import android.os.Handler;   
  13. import android.os.Message;   
  14. import android.view.View;   
  15. import android.view.View.OnClickListener;   
  16. import android.widget.Button;   
  17. import android.widget.EditText;   
  18. import com.google.android.maps.GeoPoint;   
  19. import com.google.android.maps.MapActivity;   
  20. import com.google.android.maps.MapView;   
  21.   
  22. public class GeocodingDemoActivity extends MapActivity {   
  23.   
  24.     Geocoder geocoder = null;   
  25.     MapView mapView = null;   
  26.     ProgressDialog progDialog = null;   
  27.     List<Address> addressList = null;   
  28.   
  29.     @Override  
  30.     protected boolean isLocationDisplayed() {   
  31.         return false;   
  32.     }   
  33.   
  34.     @Override  
  35.     protected boolean isRouteDisplayed() {   
  36.         return false;   
  37.     }   
  38.   
  39.     @Override  
  40.     protected void onCreate(Bundle icicle) {   
  41.         super.onCreate(icicle);   
  42.         setContentView(R.layout.main);   
  43.         mapView = (MapView) findViewById(R.id.geoMap);   
  44.         mapView.setBuiltInZoomControls(true);   
  45.         // 北京经纬度   
  46.         // 经度:116.3946533203125   
  47.         // 纬度:39.87601941962116   
  48.         int lat = (int) (39.87601941962116 * 1000000);   
  49.         int lng = (int) (116.3946533203125 * 1000000);   
  50.         GeoPoint pt = new GeoPoint(lat, lng);   
  51.         mapView.getController().setZoom(10);   
  52.         mapView.getController().setCenter(pt);   
  53.   
  54.         Button geoBtn = (Button) findViewById(R.id.geocodeBtn);   
  55.         geoBtn.setOnClickListener(new OnClickListener() {   
  56.             @Override  
  57.             public void onClick(View v) {   
  58.                 EditText loc = (EditText) findViewById(R.id.location);   
  59.                 String locationName = loc.getText().toString();   
  60.                 progDialog = ProgressDialog.show(GeocodingDemoActivity.this,   
  61.                         "Processing.....""Finding Location"truefalse);   
  62.                 findLocation(locationName);   
  63.   
  64.             }   
  65.         });   
  66.     }   
  67.   
  68.     private void findLocation(final String locationName) {   
  69.   
  70.         Thread thrd = new Thread() {   
  71.             @Override  
  72.             public void run() {   
  73.                 System.out.println("线程Name是:"  
  74.                         + Thread.currentThread().getName());   
  75.                 try {   
  76.                     // do backgrond work   
  77.                     JSONObject jo = MapUtility.getLocationInfo(locationName);   
  78.                     GeoPoint gp = MapUtility.getGeoPoint(jo);   
  79.                     Message message = uiCallback.obtainMessage();   
  80.                     message.obj = gp;   
  81.                     message.sendToTarget();   
  82.                 } catch (Exception e) {   
  83.                     e.printStackTrace();   
  84.                 }   
  85.             }   
  86.         };   
  87.         thrd.start();   
  88.     }   
  89.   
  90.     // ui thread callback handler   
  91.     private Handler uiCallback = new Handler() {   
  92.         public void handleMessage(android.os.Message msg) {   
  93.             System.out.println("线程Name是:" + Thread.currentThread().getName());   
  94.             progDialog.dismiss();   
  95.             GeoPoint pt = (GeoPoint) msg.obj;   
  96.             if (pt != null) {   
  97.                 mapView.getController().setZoom(15);   
  98.                 mapView.getController().setCenter(pt);   
  99.             } else {   
  100.                 Dialog foundNothingDlg = new AlertDialog.Builder(   
  101.                         GeocodingDemoActivity.this).setIcon()   
  102.                         .setTitle("Failed to Find Location")   
  103.                         .setPositiveButton("OK"null)   
  104.                         .setMessage("Location Not Found").create();   
  105.                 foundNothingDlg.show();   
  106.             }   
  107.         };   
  108.     };   
  109. }  
package xiaohang.zhimeng;import java.util.List;
import org.json.JSONObject;
import xiaohang.zhimeng.tools.MapUtility;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;public class GeocodingDemoActivity extends MapActivity {Geocoder geocoder = null;MapView mapView = null;ProgressDialog progDialog = null;List<Address> addressList = null;@Overrideprotected boolean isLocationDisplayed() {return false;}@Overrideprotected boolean isRouteDisplayed() {return false;}@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);mapView = (MapView) findViewById(R.id.geoMap);mapView.setBuiltInZoomControls(true);// 北京经纬度// 经度:116.3946533203125// 纬度:39.87601941962116int lat = (int) (39.87601941962116 * 1000000);int lng = (int) (116.3946533203125 * 1000000);GeoPoint pt = new GeoPoint(lat, lng);mapView.getController().setZoom(10);mapView.getController().setCenter(pt);Button geoBtn = (Button) findViewById(R.id.geocodeBtn);geoBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {EditText loc = (EditText) findViewById(R.id.location);String locationName = loc.getText().toString();progDialog = ProgressDialog.show(GeocodingDemoActivity.this,"Processing.....", "Finding Location", true, false);findLocation(locationName);}});}private void findLocation(final String locationName) {Thread thrd = new Thread() {@Overridepublic void run() {System.out.println("线程Name是:"+ Thread.currentThread().getName());try {// do backgrond workJSONObject jo = MapUtility.getLocationInfo(locationName);GeoPoint gp = MapUtility.getGeoPoint(jo);Message message = uiCallback.obtainMessage();message.obj = gp;message.sendToTarget();} catch (Exception e) {e.printStackTrace();}}};thrd.start();}// ui thread callback handlerprivate Handler uiCallback = new Handler() {public void handleMessage(android.os.Message msg) {System.out.println("线程Name是:" + Thread.currentThread().getName());progDialog.dismiss();GeoPoint pt = (GeoPoint) msg.obj;if (pt != null) {mapView.getController().setZoom(15);mapView.getController().setCenter(pt);} else {Dialog foundNothingDlg = new AlertDialog.Builder(GeocodingDemoActivity.this).setIcon(0).setTitle("Failed to Find Location").setPositiveButton("OK", null).setMessage("Location Not Found").create();foundNothingDlg.show();}};};
}


   这次我们再来看看运行效果。

cbf0ad1c-d80b-31ce-a630-0c18aae32126.jpg

7da308b7-7f8f-3fc5-bafd-8356bee25dfe.jpg

530d3218-02e4-3755-8232-0f50b32d47a1.jpg

   从下图我们能看出 查询操作在不同的线程中完成。

5cbd789e-aaa2-32cc-902f-458efe0f48a2.jpg

  最后在提醒大家如果下载源码 注意替换成自己的 密钥。

转载于:https://my.oschina.net/yl1988/blog/33030