Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to have two Google Maps in the same Android Application
    primarykey
    data
    text
    <p>I've a problem with my application. Everything work very well, but when i need to add two marker in a new istance of a Google Map is not possible because mMap = (mMapFragment).getMap() return null (i've seen with Log.i) I already have another Map in the first activity that show all the nearest Markers and it work VERY well.</p> <p>This is my DrawPathActivity</p> <pre><code>public class DrawPathActivity extends FragmentActivity { private GoogleMap mMap; private GoogleMapOptions options = new GoogleMapOptions(); private MapFragment mMapFragment; private PolylineOptions PolyOpt = new PolylineOptions(); private Polyline polyline; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mMapFragment = MapFragment.newInstance(); setContentView(R.layout.activity_draw_path); options.mapType(GoogleMap.MAP_TYPE_NORMAL) .compassEnabled(true) .scrollGesturesEnabled(true) .zoomGesturesEnabled(true); MapFragment.newInstance(options); mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1)).getMap(); android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.map1, mMapFragment); fragmentTransaction.commit(); mMap.setMyLocationEnabled(true); Intent intent = getIntent(); LatLng cpPos = new LatLng(intent.getExtras().getDouble("cp_lat"), intent.getExtras().getDouble("cp_lon")); LatLng poiPos = new LatLng(intent.getExtras().getDouble("POI_lat"), intent.getExtras().getDouble("POI_lon")); addMarkers(intent); PolyOpt.add(cpPos) .add(poiPos) .width(10) .color(Color.BLUE) .geodesic(true); polyline = mMap.addPolyline(PolyOpt); String url = makeURL(cpPos.latitude, cpPos.longitude, poiPos.latitude, poiPos.longitude); drawPath(url, true); } private void addMarkers(Intent intent) { Log.i("mMap ini", String.valueOf(mMap)); Log.i("mMapFragment", String.valueOf(mMapFragment)); LatLng cpPos = new LatLng(intent.getExtras().getDouble("cp_lat"), intent.getExtras().getDouble("cp_lon")); LatLng poiPos = new LatLng(intent.getExtras().getDouble("POI_lat"), intent.getExtras().getDouble("POI_lon")); mMap = (mMapFragment).getMap(); &lt;&lt;&lt;---- mMap IS NULL!!! Log.i("mMap", String.valueOf(mMap)); mMap.addMarker(new MarkerOptions() .position(cpPos) .icon(BitmapDescriptorFactory.fromResource(R.drawable.youarehere))); mMap.addMarker(new MarkerOptions() .position(poiPos) .title(intent.getExtras().getString("POI_name")) .icon(BitmapDescriptorFactory.fromResource(R.drawable.poi_serena))); } public static LatLng locationToLatLng(Location loc) { if(loc != null) return new LatLng(loc.getLatitude(), loc.getLongitude()); return null; } public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){ StringBuilder urlString = new StringBuilder(); urlString.append("http://maps.googleapis.com/maps/api/directions/json"); urlString.append("?origin=");// from urlString.append(Double.toString(sourcelat)); urlString.append(","); urlString .append(Double.toString( sourcelog)); urlString.append("&amp;destination=");// to urlString .append(Double.toString( destlat)); urlString.append(","); urlString.append(Double.toString( destlog)); urlString.append("&amp;sensor=false&amp;mode=driving&amp;alternatives=true"); return urlString.toString(); } public void drawPath(String result, boolean withSteps) { try { //Tranform the string into a json object final JSONObject json = new JSONObject(result); JSONArray routeArray = json.getJSONArray("routes"); JSONObject routes = routeArray.getJSONObject(0); JSONObject overviewPolylines = routes.getJSONObject("overview_polyline"); String encodedString = overviewPolylines.getString("points"); List&lt;LatLng&gt; list = decodePoly(encodedString); for(int z = 0; z&lt;list.size()-1;z++){ LatLng src= list.get(z); LatLng dest= list.get(z+1); Polyline line = mMap.addPolyline(new PolylineOptions() .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)) .width(10) .color(Color.BLUE).geodesic(true)); } if(withSteps) { JSONArray arrayLegs = routes.getJSONArray("legs"); JSONObject legs = arrayLegs.getJSONObject(0); JSONArray stepsArray = legs.getJSONArray("steps"); //put initial point for(int i=0;i&lt;stepsArray.length();i++) { Step step = new Step(stepsArray.getJSONObject(i)); mMap.addMarker(new MarkerOptions() .position(step.location) .title(step.distance) .snippet(step.instructions) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); } } } catch (JSONException e) { } } </code></pre> <p>This is the xml layout of this class</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;CheckBox android:id="@+id/satellite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/satellite" /&gt; &lt;/LinearLayout&gt; &lt;fragment android:id="@+id/map1" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Someone can help me? And can explain how the map is null? Because I can't add the two Marker on the map, and I can't finish my job.</p> <p>Thanks to all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload