Issue
I have set up a MapView Class which has Overlays (and they're working fine.)
My application is built around tabs and on my first tab, this is my code:
public void onCreate(Bundle savedInstanceState) {
Button ButtonName1;
Button ButtonName2;
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
{
ButtonName1 = (Button) findViewById(R.id.ButtonName1);
ButtonName1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:51.594748,-0.107879"));
i.setClassName("my.android.project", "my.android.project.Map"); // .Map is my MapView file
startActivity(i);
}
});
}
{
ButtonName2 = (Button) findViewById(R.id.ButtonName2);
ButtonName2_Cinema.setOnClickListener(new OnClickListener() {
public void onClick(View arg1) {
Intent ii = new Intent(Intent.ACTION_VIEW, Uri.parse("51.55748,0.07388"));
ii.setClassName("my.android.project", "my.android.project.Map");
startActivity(ii);
}
});
}
}
}
Now by clicking on those buttons, it successfully leads me onto the Map class and my overlays are displayed perfectly well, in the correct position, but it doesn't point me to that those exact latitude and longitude positions, therefore, not directly onto the overlays.
Does anyone know why?
Here's my MapClass:
public class Map extends MapActivity {
private MapView mapView;
public static int latitude1 = (int) (51.508170 * 1E6);
public static int longitude1 = (int) (-0.128017 * 1E6);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
OverlayMap itemizedOverlay = new OverlayMap(drawable, this);
GeoPoint location1 = new GeoPoint(latitude1, longitude1);
OverlayItem overlayitem = new OverlayItem(location1, "Title", "Contents");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
GeoPoint yourGeoPoint = new GeoPoint(latitude1,longitude1);
mapController.animateTo(yourGeoPoint);
mapController.setZoom(10);
}
@Override
protected boolean isRouteDisplayed() {
return true;
}
}
Solution
Are you using animateTo(GeoPoint geoPoint)
method to go to your geo point?
EDIT:
You have to do something like this in your MapActivity, you can put the code in onCreate():
GeoPoint yourGeoPoint = new GeoPoint(yourLatitude*1E6, yourLongitude*1E6);
yourMapViewControler.animateTo(yourGeoPoint);
I don't know exactly how is your MapActivity to help you better but the thing is that you have to put the code above in the MapActivity where you have access to your map controler and the new coordinates (I think in onCreate() ).
EDIT II :
I think you are not sending correctly the longitude and latitude through your intent.
You have to make use of a Bundle to do that. Please read this article for more details..
EDIT III :
Ok so you should do this to pass your geopoints to the next activity: In your button click listener you should write this:
/* create a intent to start the new activity */
Intent intent = new Intent(this, YourMapActivity.class);
/* create a bundle to pass data to your next activity */
Bundle bundle = new Bundle();
/* put the latitude and longitude values into the bundle */
bundle.putInt("latitude_key", yourLatitudeValue);
bundle.putInt("longitude_key", yourLongitudeValue);
/* include the bundle into your intent */
intent.putExtras(bundle);
/* start your MapActivity */
startActivity(intent);
Ok so now we have to get the data from the bundle and use it on your map.. The following code is for your MapActivity in onCreate() method:
/* get the bundle from the button activity */
Bundle bundle = getIntent().getExtras();
/* get the latitude and longitude based on the same key you put in the ButtonActivity to send the data */
int latitude = bundle.getInt("latitude_key", 0);
int longitude = bundle.getInt("longitude_key", 0);
/* put the data into a GeoPoint and make the animation */
GeoPoint yourGeoPoint = new GeoPoint(latitude * 1E6, longitude * 1E6);
yourMapViewControler.animateTo(yourGeoPoint);
The ButtonActivity is the activity on which you have the buttons :). Note that you have to use the same key strings in your bundle to get the correct data, just like in the example above.
I hope now everything is clear :D.
Answered By - Cata
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.