0

I am developing an android app. My app needs to show a pin on Google Maps at the time user clicks on the ListView item but I don't want to put the Google Maps into my application - instead, I want to launch it using an Intent. Is this possible?

Following is my DisplayActivity.java file. I want to call Google maps app(if app not installed it should notify user) when user clicks on any item from nameAddressList which is assigned to ListView through adapter.

DisplayActivity:

enter code herepublic class DisplayActivity extends Activity {

ListView listView;
private String tag_name; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    listView = (ListView) findViewById(R.id.list);

    Intent intent = getIntent();
    if(intent!= null)
    {
        //int imageId = intent.getIntExtra("DashboardImage",R.drawable.apartments);
        tag_name = intent.getStringExtra("DashItemName");
    }

    List<NameAddress> nameAddressList = null;
    try {
        XMLPullParserHandler parser = new XMLPullParserHandler(tag_name);
        nameAddressList = parser.parse(getAssets().open("data.xml"));
        ArrayAdapter<NameAddress> adapter =
            new ArrayAdapter<NameAddress>(this,R.layout.list_item, nameAddressList);
        listView.setAdapter(adapter);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display, menu);
    return true;
  }
}
Akshay
  • 833
  • 2
  • 16
  • 34
  • this link will help you sure http://stackoverflow.com/questions/13053352/android-how-to-launch-google-map-intent-in-android-app-with-certain-location – Sanket Shah Apr 11 '14 at 07:59

4 Answers4

1

use below snippet and try..../

String uri = "geo:"+ latitude + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
viswanadh
  • 33
  • 1
  • 5
  • @Andrain I don't have long and lat of location.My nameAddressList has name & address of the location. when user click on any string from this array. it should pass that string to Google maps and show pin for that location. Also please tell me how should I implement this. I am new to android programming. – Akshay Apr 11 '14 at 17:06
1

To show route on Google Map, Just call an intent which passes current and destination latitude and longitude.After doing this, its Google Map's job to show location. You may also show street view.

In below code, there are three parameters : current_lat, current_longi, dest_address

 final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http:/a/maps.google.com/maps?" 
+ "saddr="+ current_lat+","+current_longi + "&daddr="+dest_address ));

intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");

startActivity(intent); 

If you have current address and destination address, then you can write like this :

Uri.parse("http://maps.google.com/maps?" 
+ "saddr="+curr_address+ "&daddr="+dest_address ));

If you have current and destination latitude and longitude both then you can write like this :

Uri.parse("http://maps.google.com/maps?" 
+ "saddr="+ current_lat+","+current_longi + "&daddr="+ destt_lat+","+dest_longi  ));

When you call this intent, Google Map shows option whether to draw route by bus or by walk.

Namrata
  • 1,683
  • 1
  • 17
  • 28
  • I don,t want to route it. My nameAddressList has name & address of the location. when user click on any string from this array. it should pass that string to Google maps and show pin for that location. Also please tell me how should I implement this. I am new to android programming. – Akshay Apr 11 '14 at 17:03
0

Try this code,It was worked for me i think will you too.Here you have to put map package name.

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
Mohan
  • 311
  • 8
  • 15
0

You can use this to get latitude and longitude from Address

Geocoder coder = new Geocoder(this);
List<Address> address;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address == null) 
    {
        return null;
    }
    Address location = address.get(0);
    location.getLatitude();
    location.getLongitude();
   }

After getting Lat&Long you can pass this to google map with the help of following

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);

Hope it will help you.

Sanket Shah
  • 4,352
  • 3
  • 21
  • 41