android - 在 MapView-Android 中使用 LocationName 为某个位置设置动画

标签 android android-mapview

我正在尝试在 MapView 中使用作为“国家/地区名称”的“位置名称”为某个位置设置动画。我收到异常: IOException: Service not Available, Couldn't get connection factory client.

如果您看不到图片,则异常是::IOException: Service not Available with error Couldn't get connection factory client

我使用的代码如下:

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

list 文件如下:

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

希望得到解决。

提前致谢

有可能。

最佳答案

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

一个简单的例子:

list :

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

主.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 key

关于android - 在 MapView-Android 中使用 LocationName 为某个位置设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529358/

相关文章:

android - 在获取 Google map fragment 时遇到问题调用 onMapReady

android - 更改 MapView 中的缩放控件

android - 我们可以在 Android 中使用 SOAP UI 与客户端和服务器通信吗?

android - 适用于 Android 的 PDF 库 - PDFBox?

android - 在 Android Webview 中从浏览器打开链接

android - runOnFirstFix 实际上没有在第一个位置修复上运行

android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData

android - 何时在 Gradle 插件中应用自定义 Android 扩展?

android - 形状的渲染顺序

android - 如何从主视图访问 map fragment