Reputation:
hi i have to implement Google map v2 in my application an it's perfect working. but my problem i have to send one Marker position from one activity to another activity and then add marker into Google map but i don't know it's not.
Post my code below.
public class BasicMapActivity extends FragmentActivity{
private GoogleMap mMap;
AlertMessages messages;
String latitude;
double lat = 0, lng = 0;
String longitude ;
String title = "", city = "";
static boolean is_back_pressed = false;
ImageView img;
GPSTracker gps;
RelativeLayout rel;
Marker Contactloc;
LatLng contactLoc;
Button ib_back;
static boolean is_window_pressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo_new);
img = (ImageView) findViewById(R.id.popup_h);
img.setVisibility(View.INVISIBLE);
gps = new GPSTracker(getParent());
messages = new AlertMessages(getParent());
Bundle b = getIntent().getExtras();
title = b.getString("title");
city = b.getString("City");
latitude = b.getString("latitude");
longitude = b.getString("longitude");
back_layout=(LinearLayout)findViewById(R.id.back_lay);
ib_back=(Button)findViewById(R.id.ib_back_music);
System.out.println("Lat:::" + latitude);
System.out.println("Long:::" + longitude);
setUpMapIfNeeded();
ib_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
is_back_pressed = true;
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.onBackPressed();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try{
System.out.println("Map Pause state");
if(mMap!=null){
mMap.clear();
mMap=null;
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.onBackPressed();
}
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getParent());
if (status == ConnectionResult.SUCCESS) {
setUpMapIfNeeded();
mMap.setMyLocationEnabled(true);
} else {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getParent(),
requestCode);
dialog.show();
}
}
protected LocationManager locationManager;
LatLng pos=null;
private void setUpMapIfNeeded() {
System.out.println("Setup If Needed");
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1)).getMap();
mMap.setMyLocationEnabled(true);
boolean isInternetPresent = isConnectingToInternet();
if (!isInternetPresent) {
// Internet Connection is not present
Toast.makeText(getParent(), "Internet Connection Error!",Toast.LENGTH_SHORT).show();
//messages.showAlertDialog(getParent(),
// "Internet Connection Error",
//"Please connect to working Internet connection");
// stop executing code by return
// return;
} else {
locationManager = (LocationManager) getParent()
.getSystemService(LOCATION_SERVICE);
// getting GPS status
if (!locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showSettingsAlert();
}
if (gps.canGetLocation()) {
lat = gps.getLatitude();
lng = gps.getLongitude();
pos = new LatLng(lat, lng);
if (lat == 0.0 && lng == 0.0) {
Toast.makeText(getParent(), "Can't find location",
Toast.LENGTH_SHORT).show();
} else {
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark_blue))
.position(pos)
.title("Current Location"));
double trans = 0.3;
for (int k = 100; k <= 500;) {
// draw circle
int radiusM = k;
int d = k; // diameter
Bitmap bm = Bitmap.createBitmap(d, d,
Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint();
p.setColor(getResources().getColor(
android.R.color.darker_gray));
c.drawCircle(d / 2, d / 2, d / 2, p);
// generate BitmapDescriptor from circle
// Bitmap
BitmapDescriptor bmD = BitmapDescriptorFactory
.fromBitmap(bm);
// mapView is the GoogleMap
mMap.addGroundOverlay(new GroundOverlayOptions()
.image(bmD)
.position(pos, radiusM * 2,
radiusM * 2)
.transparency((float) trans));
k = k + 100;
trans = trans + 0.1;
}
}
} else {
Toast.makeText(getParent(), "Can't find location",
Toast.LENGTH_SHORT).show();
}
}
mMap.getUiSettings().setCompassEnabled(true);
mMap.setTrafficEnabled(true);
mMap.getUiSettings().setTiltGesturesEnabled(true);
mMap.getUiSettings().setRotateGesturesEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
setUpMap();
} // Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
setUpMap();
}
}
private void setUpMap() {
System.out.println("Setup Map");
try{
contactLoc=new LatLng(latitude, longitude);
Contactloc=mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
.position(new LatLng(latitude, longitude)).title(title)
.snippet(city));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 18.0f));
}catch(Exception ew){
ew.printStackTrace();
}
}
}
please some one help me!
Upvotes: 1
Views: 183
Reputation: 47807
Add Marker like below
Contactloc=mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
.position(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)).title(title)
.snippet(city));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 18.0f));
You need to Convert your latitude and longitude String
values into Double
. and AFAIK you said no not any error. your app defiantly crash over here with some logcat.
Upvotes: 2