0

I am new to building android apps and I've been trying to open a new activity from a row in a list where each activity displays google map of each row. So far I have managed to open a new activity by clicking the row, but I have no idea how to insert Google map on them. I have downloaded my Google api key and saved it on my AndroidManifest.

My code looks looks like this that opens the list:

package com.MaheshGurung.androidapp;

import android.app.ListActivity;



import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class secondActivity extends ListActivity {

@Override

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // storing string resources into Array

     String []Buildings = getResources().getStringArray(R.array.Buildings);

     // Binding resources Array to ListAdapter
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));

}
 protected void onListItemClick(ListView lv, View v, int position, long id){
     super.onListItemClick(lv, v, position, id);

Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);

 }
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

And the code which opens new activity for each row looks like this:

package com.MaheshGurung.androidapp;

import android.app.ListActivity;



import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class secondActivity extends ListActivity {

@Override

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // storing string resources into Array

     String []Buildings = getResources().getStringArray(R.array.Buildings);

     // Binding resources Array to ListAdapter
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));

}
 protected void onListItemClick(ListView lv, View v, int position, long id){
     super.onListItemClick(lv, v, position, id);

Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);

 }
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

and .xml file looks like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_textview"/>
<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_below="@+id/my_textview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</RelativeLayout>

It would be very helpful to know where to write code for google map or any other suggested method.

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Mesh
  • 19
  • 1
  • 8
  • Google has a API documentation about Google Maps, check it out here: http://developer.android.com/google/play-services/maps.html That's all I can do for you, because you're question is a bit vague – tim687 Jan 28 '15 at 16:14

1 Answers1

0

What you need to do is two things:

  1. When you click on the ListView item, it needs to pass information to the second activity.
  2. The second activity needs to receive the instructions, and load up the appropriate map.

As an aside, this would probably work better using Fragments, if you ever want to have the list on the same screen as the map (Say, for a tablet), then it would make it easier.

For the first one, there's plenty of sources out there. I'll point you to this question: How do I pass data between Activities in Android application? . You have similar code already, I see. I would suggest rather than passing a position, pass lat/ lng/ zoom level, or something similar, so the map doesn't need to figure out anything. That makes it easier to re-use in other components.

For the second, you should refer to the Google Maps API, which shows how to change the view. Actually, what you will want to do is to change the camera position, they provide the following example code. Just receive the lat/lng boundaries in the second activity, get the mMap reference as specified in the API, and pass it similar to how is managed below.

private GoogleMap mMap;
// Create a LatLngBounds that includes Australia.
private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

// Set the camera to the greatest possible zoom level that includes the
// bounds
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));
Community
  • 1
  • 1
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142