在MapView Android中使用LocationName设置位置动画

在MapView Android中使用LocationName设置位置动画,android,android-mapview,Android,Android Mapview,我试图在地图视图中使用“地点名称”(即“国家名称”)来设置地点的动画。我得到一个例外: 如果无法看到pic,则异常为::IOException:Service not Available,错误为无法获取连接工厂客户端 我使用的代码如下所示: public class ShowMapView extends MapActivity { MapView mapView; MapController mapController; String Country; @Override

我试图在地图视图中使用“地点名称”(即“国家名称”)来设置地点的动画。我得到一个例外:

如果无法看到pic,则异常为::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;
    }
}
公共类ShowMapView扩展了MapActivity
{
地图视图;
地图控制器;
弦国;
@凌驾
创建时的公共void(Bundle bundleMap)
{
super.onCreate(bundleMap);
setContentView(右布局显示地图);
mapView=(mapView)findViewById(R.id.mapView);
LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
View zoomView=mapView.GetZoomControl();
zoomLayout.addView(zoomView,
新建LinearLayout.LayoutParams(
LayoutParams.WRAP_内容,
LayoutParams.WRAP_内容);
mapView.SetBuilTinZoomControl(真);
Intent=this.getIntent();
国家=意向。getStringExtra(“国家”);
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
Log.v(“GEOCODER”,GEOCODER.toString());
尝试
{
Log.v(“MAP”、“TRY BLOCK”);
列表,但它不能正常工作

如果我遗漏了什么,请告诉我

我遇到的问题和你的一样

希望找到解决办法

提前感谢,


Haps.

StackOverflow上有许多关于该异常的帖子,例如:。您的代码可能根本没有问题,Geocoder会根据服务器可用性不时抛出该异常。您需要处理该异常,可能需要返回到使用Google Places API,如以下评论所示:

一旦有了地质点,就可以使用MapController.animateTo(地质点)——

一个简单的例子:

舱单:


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

}
package com.rd.TestGeocoder;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.Intent;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.Bundle;
导入android.util.Log;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
公共类TestGeocoderActivity扩展了MapActivity{
/**在首次创建活动时调用*/
地图视图;
地图控制器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串country=“澳大利亚”;
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mapController=mapView.getController();
Intent=this.getIntent();
if(intent.getStringExtra(“国家”)!=null){
国家=意向。getStringExtra(“国家”);
}
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
Log.v(“GEOCODER”,GEOCODER.toString());
试一试{
Log.v(“MAP”、“TRY BLOCK”);
列表地址=geocoder.getFromLocationName(国家,2);
Log.v(“地图”、“国家:+国家”);
if(address.size()>0){
对数v(“地图”、“地质点p”);
地质点p=新的地质点(
(int)(address.get(0.getLatitude()*1E6),
(int)(address.get(0.getLongitude()*1E6));
Log.v(“映射”,“L&L完成:”+p.toString());
mapController.animateTo(p);
Log.v(“映射”、“动画到P”);
mapView.invalidate();
Log.v(“MAP”,“Invalidate()”);
}
}捕获(IOE异常){
Log.v(“映射”、“捕捉块”);
e、 printStackTrace();
}
}
@凌驾
受保护的布尔值isRouteDisplayed(){
Log.v(“地图”、“路线:假”);
返回false;
}
}

显然,您需要更改包名并填写api密钥,StackOverflow上有许多关于该异常的帖子,例如:。您的代码可能根本没有问题,地理编码器会根据服务器可用性不时抛出该异常。您需要处理该异常,可能是通过返回到使用Google Places API,如本评论所示:

一旦有了地质点,就可以使用MapController.animateTo(地质点)——

一个简单的例子:

舱单:


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

}
package com.rd.TestGeocoder;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.Intent;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.Bundle;
导入android.util.Log;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
公共类TestGeocoderActivity扩展了MapActivity{
/**在首次创建活动时调用*/
地图视图;
地图控制器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串country=“澳大利亚”;
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mapController=mapView.getController();
Intent=this.getIntent();
if(intent.getStringExtra(“c
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));

}

}