2

I just about got my maps working in my app. I'm having a problems. when the place comes up on the maps. I want to be able to click on the place and have it bring it up in the proper Google Maps App. so if needed you can navigate to the place.

So I have a map with a place on a marker. I want to click on the marker and then have the address in the Google maps. this is so that if people to navigate they can just click on it and then get directions.

java code is as:

public class showroommap extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

} 

Is this possible?

MORE INFO: I want to click on the marker that is showed in my app. once clicked on the marker it will go to Google Maps and will direct to it as required.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
x iHarpzZ
  • 151
  • 3
  • 16

2 Answers2

3

If I understand correctly what you want is to send an intent to the Google Maps app with the latitude and longitude so the user can the navigate to the specific locaiton. Here is how I implemented it in my app:

Intent navigation = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +latlong.latitude+","+latlong.longitude));
        startActivity(navigation);

latlong is of type LatLng.

UPDATE 1 - to listen to clicks on Marker

public class showroommap extends Activity implements onMarkerClickListener {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
map.setOnMakerClickListener(this); //Register this Activity to the onMarkerClickListener
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

@Override
 public boolean onMarkerClick(final Marker marker) {

    if (marker.equals(kiel)) 
    {
        //handle click here
     return true;
    }
    else if (marker.equals(hamburg)) 
    {
        //handle click here
        return true;
    }
    return false;
}

} 
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • hi, yeah sort of i want to actually bring up the map in my app. but then when i click on the marker want it to go to maps and bring up directions – x iHarpzZ Sep 03 '13 at 14:42
  • Yeah, so what you are going to do is set GoogleMap.setOnMarkerClickListener(); Then on onMarkerClick(Marker marker) you will set and launch the Intent. You can reference a specific Marker inside onMarkerClick(Marker marker) by using an if statement and matching the title of the Marker that gets passed to the method with the name title you gave to the marker or by simply testing if the Marker is the same marker using marker.equals(kiel) for example. – Emmanuel Sep 03 '13 at 14:46
  • hi, theanks very much for your response... is there an example of this anywhere as im confused – x iHarpzZ Sep 03 '13 at 14:55
  • Here is another post I found with an implementation http://stackoverflow.com/questions/14226453/google-maps-api-v2-how-to-make-markers-clickable. I am going to edit my answer to help you out a bit. – Emmanuel Sep 03 '13 at 14:59
  • Please mark this question as answered if it solved your problem. – Emmanuel Jan 27 '14 at 13:37
0

here maybe you can use something like this

public class showroommap extends Activity implements OnMarkerClickListener{
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));
map.setOnMarkerClickListener(this); 
//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

} 

 @Override
public boolean onMarkerClick(Marker arg0) {
final Context mContext = this;
final LatLng now = arg0.getPosition();
AlertDialog.Builder course = new AlertDialog.Builder(mContext);
    course.setNegativeButton("On Foot", new OnClickListener(){


        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s", now.latitude, now.longitude, "&mode=w")));
            mContext.startActivity(i);
        } 

    });

    course.setNeutralButton("By Car", new OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s",  now.latitude, now.longitude, "&mode=d")));
            mContext.startActivity(i);
        }

    });
    course.setPositiveButton("On Bike", new OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s",  now.latitude, now.longitude, "&mode=b")));
            mContext.startActivity(i);
        }

    });
course.show();
return false;
}
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • He wants to be able to click on the Marker and have the Marker launch the Google Maps app... – Emmanuel Sep 03 '13 at 14:55
  • not google maps, google navigator, and its just alert dialog to put in marker click, im thinking about taking it off he said its not what you want – JRowan Sep 03 '13 at 14:58
  • there you go, i edited your question for a working example in my answer – JRowan Sep 03 '13 at 15:17
  • `public boolean onMarkerClick(Marker arg0) {` error says it must be boolean. if i change it to boolean it says it must be void. so it circling – x iHarpzZ Sep 03 '13 at 15:24
  • Or it says add a return statement – x iHarpzZ Sep 03 '13 at 15:26
  • The method needs to return a boolean value. – Emmanuel Sep 03 '13 at 15:31
  • sorry i just put in return false – JRowan Sep 03 '13 at 15:33
  • I think that if you return false, the listener will perform its normal behavior which is animating to the marker and displaying the info. I think returning true should work if it doesn't with false. – Emmanuel Sep 03 '13 at 15:35
  • oh my god. thanks you very much guys. thanks for all your input it works perfectly. also one more thing. what if i just want car only an remove other 2. and also is there a way to go into maps and not navigation? thanks again – x iHarpzZ Sep 03 '13 at 16:19
  • i couldnt help you with maps, and the buttons just delete the other two – JRowan Sep 03 '13 at 16:21
  • thanks anyway. it must be something with `"google.navigation:ll=%s,%s%s"` instead of navigation its probably maps i will try and figure it out. thanks again – x iHarpzZ Sep 03 '13 at 16:31