Issue
I am trying to animate in the MapView to a location by using "Location Name" which is a "Country Name". I am getting the exception:
If you can't see the pic then the exception is:: IOException: Service not Available with error Couldn't get connection factory client.
the code I am using is as below:
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;
}
}
The manifest file is as below::
<?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>
I have worked according this tutorial, but it doesn't work properly.
Let me know if I am missing something.
I am getting the problem as same as in this blog
Hoping for a solution.
Thanks in advance,
Haps.
Solution
Many posts on StackOverflow about that exception, such as: Service not Available - Geocoder Android . It may not be a problem with your code at all, the Geocoder will throw this exception from time to time based on server availability. You will need to handle that exception, possibly by falling back to using the Google Places API, as illustrated in this comment: https://stackoverflow.com/a/9173488/429047
Once you have your GeoPoint, you can use MapController.animateTo(Geopoint) - http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapController.html
A simple example:
Manifest:
<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;
}
}
Obviously you would need to change the package name and fill in your api key
Answered By - Mo Kargas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.