【问题标题】:Animate to a location by using LocationName in MapView-Android在 MapView-Android 中使用 LocationName 为某个位置设置动画
【发布时间】:2012-03-07 15:43:04
【问题描述】:

我正在尝试使用“国家/地区名称”的“位置名称”在 MapView 中为某个位置设置动画。我得到了例外:

如果您看不到图片,则异常是:IOException: Service not Available 错误无法获取连接工厂客户端

我使用的代码如下:

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中的相同

希望有解决方案。

提前致谢,

哈哈。

【问题讨论】:

    标签: android android-mapview


    【解决方案1】:

    StackOverflow 上有很多关于该异常的帖子,例如:Service not Available - Geocoder Android。您的代码可能根本不是问题,Geocoder 会根据服务器可用性不时抛出此异常。您将需要处理该异常,可能通过回退使用 Google 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 密钥

    【讨论】:

    • 感谢您的回复。此代码给出了该异常,但也没有帮助我到达所需的位置。
    • 我写了一个非常简单的地理编码器测试。我注意到你实际上并没有设置你的 mapController。 mapView 分配后,你应该设置 mapController = mapView.getController();我觉得这是一个问题,再加上我怀疑您对意图对象的 getStringExtra 调用可能返回 null。此外,您的国家/地区变量应为小写。我已经发布了我非常简单的示例。
    • 感谢您的回复,我已根据您的要求调整了我的代码,但异常仍然相同,它执行代码到 TRY BLOCK 并在执行 "List
      address = 时进入 CATCH 块geocoder.getFromLocationName(国家, 2);"因此这条线没有执行,现在它也停止显示它之前显示的地图。
    • 如果您只是复制并粘贴了我添加的代码而没有填写您的 API 密钥,那么是的,地图将是空白的。我没有得到例外,可能是您所在位置的服务实际上不可用。
    • 我没有复制你的代码,实际上模拟器昨天没有访问互联网,它没有显示地图但它现在可以工作了。问题还是一样的。获取异常“服务不可用”和错误“无法获取连接工厂客户端”。它只是赶上了代码“List
      address = geocoder.getFromLocationName(country, 2);”
    【解决方案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 代码并使用 Mo Kargas 的 答案中的所有 xml 文件代码。

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多