2012-03-02 48 views
1

我想通過使用「位置名稱」是「國家/地區名稱」來將MapView設置爲位置。我得到異常: IOException: Service not Available, Couldn't get connection factory client.通過在MapView中使用位置名稱動畫到位置-Android

如果你不能看到那麼PIC的例外是:: IOException異常:服務不可用錯誤無法獲取連接工廠客戶

我使用的代碼如下:

public class ShowMapView extends MapActivity 
{ 
    MapView mapView; 
    MapController mapController; 

String Country; 

@Override 
public void onCreate(Bundle bundleMap) 
{ 
    super.onCreate(bundleMap); 
    setContentView(R.layout.show_map); 

    mapView = (MapView)findViewById(R.id.MapView); 

    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
      new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    mapView.setBuiltInZoomControls(true); 


    Intent intent = this.getIntent(); 
    Country = intent.getStringExtra("Country"); 

    Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    Log.v("GEOCODER", geocoder.toString()); 

    try 
    { 
     Log.v("MAP", "TRY BLOCK"); 
     List<Address> address = geocoder.getFromLocationName(Country, 2); 
     Log.v("MAP", "Country: " + Country); 
     if(address.size() > 0) 
     { 
      Log.v("MAP", "GeoPoint p"); 
      GeoPoint p = new GeoPoint((int)(address.get(0).getLatitude() * 1E6), 
             (int)(address.get(0).getLongitude() * 1E6)); 
      Log.v("MAP", "L&L done"); 
      mapController.animateTo(p);   Log.v("MAP", "Animate to P"); 
      mapView.invalidate();    Log.v("MAP", "Invalidate()"); 
     } 
    } 
    catch(IOException e) 
    { 
     Log.v("MAP", "CATCH BLOCK"); 
     e.printStackTrace(); 
    } 
} 
@Override 
protected boolean isRouteDisplayed() 
{ 
    Log.v("MAP", "ROUTE: false"); 
    return false; 
    } 
} 

清單文件是如下::

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.project.LawSource" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 


    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

     <uses-library android:name="com.google.android.maps"/> 

     <activity 
      android:name=".GetCountry" 
      android:label="@string/app_label" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="ShowMapView" 
      android:label="@string/app_name"> 

     </activity> 
    </application> 

</manifest> 

我照着this tutorial工作,但它不能正常工作。

讓我知道如果我失去了一些東西。

我得到的問題,相同this blog

希望有一個解決方案。

在此先感謝,

Haps。

回答

5

StackOverflow上有關該異常的許多帖子,如:Service not Available - Geocoder Android。根本不存在您的代碼問題,Geocoder會根據服務器的可用性不時拋出此異常。您將需要處理該異常,可能是回落到使用谷歌Places API的,因爲在此評論說明:https://stackoverflow.com/a/9173488/429047

一旦你有你的GeoPoint,您可以使用MapController.animateTo(GeoPoint對象) - http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapController.html

一個簡單的例子:

清單:

<uses-sdk android:minSdkVersion="10" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 


<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 

    <uses-library android:name="com.google.android.maps"/> 

    <activity 
     android:name=".TestGeocoderActivity" 
     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> 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:apiKey="<YOUR API KEY HERE>" 
     android:clickable="true" 
     android:drawingCacheQuality="auto" 
     android:enabled="true" > 
    </com.google.android.maps.MapView> 

</LinearLayout> 

TestGeocoderActivity.java

package com.rd.TestGeocoder; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.util.Log; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

public class TestGeocoderActivity extends MapActivity { 
    /** Called when the activity is first created. */ 
    MapView mapView; 
    MapController mapController; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String country = "Australia"; 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mapController = mapView.getController(); 

     Intent intent = this.getIntent(); 
     if (intent.getStringExtra("country") != null) { 
      country = intent.getStringExtra("country"); 
     } 

     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     Log.v("GEOCODER", geocoder.toString()); 

     try { 
      Log.v("MAP", "TRY BLOCK"); 
      List<Address> address = geocoder.getFromLocationName(country, 2); 
      Log.v("MAP", "Country: " + country); 
      if (address.size() > 0) { 
       Log.v("MAP", "GeoPoint p"); 
       GeoPoint p = new GeoPoint(
         (int) (address.get(0).getLatitude() * 1E6), 
         (int) (address.get(0).getLongitude() * 1E6)); 
       Log.v("MAP", "L&L done:"+p.toString()); 
       mapController.animateTo(p); 
       Log.v("MAP", "Animate to P"); 
       mapView.invalidate(); 
       Log.v("MAP", "Invalidate()"); 
      } 
     } catch (IOException e) { 
      Log.v("MAP", "CATCH BLOCK"); 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     Log.v("MAP", "ROUTE: false"); 
     return false; 
    } 

} 

很明顯,你將需要更改包名和API密鑰

+0

感謝您的回覆。 此代碼給出了這個異常,但也沒有幫助我到達期望的位置。 – Harpreet 2012-03-05 10:47:44

+0

我寫了一個非常簡單的地理編碼器測試。我注意到你實際上並沒有設置你的mapController。在mapView被分配後,你應該設置mapController = mapView.getController();我有一種感覺是一個問題,加上我懷疑你的getStringExtra對intent對象的調用可能返回null。另外,你的國家變量應該是小寫的。我發佈了我非常簡單的例子。 – 2012-03-05 11:33:53

+0

感謝您的回覆,我根據您的調整了我的代碼,但異常仍然是相同的,它執行代碼到TRY BLOCK並執行「列表

address = geocoder.getFromLocationName(country,2)」時執行CATCH Block。 「因此該行不執行,現在它也停止顯示之前顯示的地圖。 – Harpreet 2012-03-06 04:41:38

2

最後它的工作填滿,我可以動畫到一個位置, 這裏是完整的代碼。

public class MapShowUsingJson extends MapActivity 
{ 
    MapView mapView; 
    MapController mapController; 

    String country; // Value of country is coming from other activity using Intent. 

@Override 
public void onCreate(Bundle bundle) 
{ 
    super.onCreate(bundle); 
    setContentView(R.layout.show_map);  
    mapView = (MapView)findViewById(R.id.MapView); 
    //mapView.setBuiltInZoomControls(true); 
    mapController = mapView.getController(); 

    Intent intent = this.getIntent(); 
    if(intent.getStringExtra("Country") != null) 
     country = intent.getStringExtra("Country"); 
    else   
     Log.v("Intent Value", "Country:NULL");  

    Log.v("Country=", country); 

    JSONObject object = getLocationInfo(country); 

    GeoPoint p = getGeoPoint(object); 

    mapController.animateTo(p); 
    mapController.setZoom(4); 
    mapView.invalidate();  
} 

@Override 
protected boolean isRouteDisplayed() 
{ 
    return false; 
} 

public static JSONObject getLocationInfo(String address) 
{ 
    address = address.replaceAll(" ","%20"); 
    HttpGet httpGet = new HttpGet("http://maps.google." 
      + "com/maps/api/geocode/json?address=" + address 
      + "&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; 
} 


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)); 

} 

} 

使用此Java代碼和使用所有的XML文件的代碼從莫Kargas的解答。