MSr
MSr

Reputation: 288

Google Maps API V2 in listview row

I have a ListView and each row it should contain a map. If I try to show a map n a FragmentActivity, It works perfectly. But my main aim is to show a map on each row of a ListView. I'm moving the codes to my adapter and changing layouts but It does not work.

Here are my codes: My Adapter:

public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if(view ==null){
            ...
            view = holder.layout;
        }

        ..
        GoogleMap map = ((SupportMapFragment) StreamActivity.streamFragmentManager.findFragmentById(R.id.mapevidence)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        double latitude = 17.385044;
        double longitude = 78.486671;
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
        map.addMarker(marker);
        float cameraZoom = 18;
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), cameraZoom));

        return view;
    }

My layout for each row of ListView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
        <fragment 
        android:id="@+id/mapevidence"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment" />
 </RelativeLayout>

LogCat:

03-27 14:05:14.138: E/AndroidRuntime(25049): FATAL EXCEPTION: main
03-27 14:05:14.138: E/AndroidRuntime(25049): android.view.InflateException: Binary XML file line #22: Error inflating class fragment
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at com.example.StreamAdapter.getView(StreamAdapter.java:59)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.ListView.makeAndAddView(ListView.java:1860)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.ListView.fillDown(ListView.java:687)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.ListView.fillFromTop(ListView.java:748)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.ListView.layoutChildren(ListView.java:1676)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.AbsListView.onLayout(AbsListView.java:2037)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.View.layout(View.java:14099)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.View.layout(View.java:14099)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.View.layout(View.java:14099)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.View.layout(View.java:14099)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.View.layout(View.java:14099)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.Choreographer.doFrame(Choreographer.java:548)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.os.Handler.handleCallback(Handler.java:725)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.os.Looper.loop(Looper.java:153)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.app.ActivityThread.main(ActivityThread.java:5336)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at java.lang.reflect.Method.invokeNative(Native Method)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at java.lang.reflect.Method.invoke(Method.java:511)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at dalvik.system.NativeStart.main(Native Method)
03-27 14:05:14.138: E/AndroidRuntime(25049): Caused by: java.lang.IllegalArgumentException: Binary XML file line #22: Duplicate id 0x7f050037, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
03-27 14:05:14.138: E/AndroidRuntime(25049):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-27 14:05:14.138: E/AndroidRuntime(25049):    ... 46 more

Line22 on xml is the beginning of the fragment.

Upvotes: 2

Views: 4008

Answers (2)

Vinil Chandran
Vinil Chandran

Reputation: 1561

Use static map API instead of Map fragment. You can implement it using web view. First of all replace the map fragment with a webview. the follow the below steps.

Create an html file static_map.html inside html folder in asset folder

its content may be like the below

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
    html,body{
        padding:0;
        margin:0;
    }
    .map{
        position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    }
</style>
</head>

<body>
<img class="map" src="REPLACE_HERE" >
</body>
</html>

The programmatically create a static map URL and replace REPLACE_HERE string with it. The load it on the web view. This will reduce our effort very much.

The code is as follows

try {
    String url ="https://maps.googleapis.com/maps/api/staticmap?";
    url+="&zoom=13";
    url+="&size=600x300";
    url+="&maptype=roadmap";
    url+="&markers=color:green%7Clabel:G%7C"+latitude+", "+longitude;
    url+="&key="+ YOUR_GOOGLE_API_KEY;

    InputStream is = context.getAssets().open("html/static_map.html");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    String str = new String(buffer);
    str = str.replace("REPLACE_HERE", url);
    wv_map.getSettings().setJavaScriptEnabled(true);
    wv_map.loadDataWithBaseURL("", str, "text/html", "UTF-8", "");
} catch (IOException e) {

}

Need to enable and the static API in google console for this.

Upvotes: 1

Jorge Arimany
Jorge Arimany

Reputation: 5882

You should use a MapView instead of a MapFragment

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView

use this in your xml:

 <com.google.android.gms.maps.MapView android:id="@+id/mapevidence"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

and this in your code, you should pass the view events to the mapview, the fragment does it automatically, but using a view you should put more code:

MapView mapView;
GoogleMap map;
...
mapView = (MapView) v.findViewById(R.id.mapevidence);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
....
@Override
public void onMapReady(GoogleMap googleMap)
{
    map = googleMap;
    try {
            MapsInitializer.initialize(NewsDetail.this);
        }
    catch (Exception e) 
       {
            ...
        }
....
@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

Upvotes: 3

Related Questions