How to display latitude and longitude on Google Maps - android

I pass data of longitude and latitude from one activity to another activity, and it is successful, but how can I after received this data Display it on Google Maps as a location so the user will be see location in google map not see the number of longitude and latitude.
Note I pass data by use getExtra() and putExtra()
If anyone know solution please help me
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Main3Activity.this, MapsActivity.class);
intent.putExtra("Latitude", Latitude);
intent.putExtra("Longitude", Longitude);
startActivity(intent);
}
});
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = getIntent();
final String Latitude=i.getStringExtra("Latitude");
final String Longitude=i.getStringExtra("Longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Change
final Double Latitude=i.getStringExtra("Latitude");
final Double Longitude=i.getStringExtra("Longitude");
to
final String Latitude=i.getDoubleExtra("Latitude");
final String Longitude=i.getDoubleExtra("Longitude");
Change LatLng sydney = new LatLng(-34, 151); to LatLng sydney = new LatLng(Latitude, Longitude);
Try this code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Double Latitude=0.0;
Double Longitude=0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = getIntent();
Latitude=i.getDoubleExtra("Latitude",0.0);
Longitude=i.getDoubleExtra("Longitude",0.0);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(Latitude, Longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Related

How to move camera and marker in google map to data of Latitude and Longitude received by intent.putExtra Android

I have pass data of latitude and longitude from (activtiy1 ) to(activtiy2 ) by intent.putExtra.
It works, but location is default location it view in marker and camera in sydney , and not according to data that I passes from the first activity to the second activity.
The first activity
public class Main3Activity extends AppCompatActivity {
TextView textView4;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView4 =(TextView)findViewById(R.id.textView4);
button2=(Button)findViewById(R.id.button2);
Intent i = getIntent();
final String Latitude=i.getStringExtra("Latitude");
final String Longitude=i.getStringExtra("Longitude");
textView4.setText(textView4.getText() + Longitude+Longitude );
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Main3Activity.this, MapsActivity.class);
intent.putExtra("Latitude", Latitude);
intent.putExtra("Longitude", Longitude);
startActivity(intent);
}
});
}
}
The second activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Double Latitude=0.0;
Double Longitude=0.0;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = getIntent();
Latitude=i.getDoubleExtra("Latitude",0.0);
Longitude=i.getDoubleExtra("Longitude",0.0);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(Latitude, Longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
example of the data I pass to clarify problem \
And example of problem
what is there problem ? anyone know the solution for it ?
You should change
final String Latitude=i.getStringExtra("Latitude");
final String Longitude=i.getStringExtra("Longitude");
to
Latitude=i.getDoubleExtra("Latitude",0.0);
Longitude=i.getDoubleExtra("Longitude",0.0);
like in MapsActivity. Don't forget to initialize it.
Everywhere you get Latitude and Longitude value, they should be in Double not String.

My map on click listner is not working

public class Returnmapdet extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMapLongClickListener,GoogleMap.OnCameraMoveListener,GoogleMap.OnMapClickListener {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_returnmapdet);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
/* String[] latLng = "-34.8799074,174.7565664".split(",");
double latitude = Double.parseDouble(latLng[0]);
double longitude = Double.parseDouble(latLng[1]);
LatLng location = new LatLng(latitude, longitude);*/
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
#Override
public void onMapLongClick(LatLng point) {
mMap.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
#Override
public void onCameraMove() {
mMap.setOnCameraMoveListener(this);
}
#Override
public void onMapClick(LatLng latLng) {
mMap.setOnMapClickListener(this);
Toast.makeText(getApplicationContext(), "tha",
Toast.LENGTH_LONG).show();
}
}
when i launch the map and when i click the map it doesn't toasting me any thing.
Set onClickListener on Map object inside onMapReady
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
}
remove mMap.setOnMapClickListener(this); from onMapClick method, its unnecessary.
Happy Coding.
you implement your interface but you should add this code
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnMapClickListener(this);
to add callback to your activity.
goodluck

Access GoogleMap object of one Activity in another Activity

I extend my app with google map activity. it created a mapclass and extends FragmentActivity implements OnMapReadyCallback.
the onMapReadyCallBack return GoogleMap object as below code.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I want to access mMap on my MainActivity class to do some operation. i create a button with onclick attribute to gotolocation method. but it return error becuase the map object is null; which is the proper way to access the GoogleMap object in my activity class.
public void gotoLocation(View v){
if (mMap != null){
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Kabul"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}else{
Log.d("Map object", "It si null==============");
}
}
Keep in mind that only one Activity at a time can be shown to the user, so there is no need to access the mMap variable from MainActivity.
Assuming you want to open up MapsActivity when the gotoLocation() button is clicked, you can pass the data associated with the location from MainActivity over to MapsActivity.
First, prepare the LatLng and description in MainActivity in order to send it to MapsActivity when the button is clicked:
public void gotoLocation(View v){
LatLng goToLocation = new LatLng(34.5392354, 69.1378334);
Bundle args = new Bundle();
args.putParcelable("latLon", goToLocation);
args.put("desc", "Marker in Kabul");
Intent i = new Intent(this, MapsActivity.class);
i.putExtras(args);
startActivity(i);
}
Then, when MapsActivity is opened, it will retrieve the info, and modify the Marker accordingly:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
LatLng mLatLng;
String mDescription;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
//Get the data sent from MainActivity
Intent intent = getIntent();
mLatLng = intent.getParcelableExtra("latLon");
mDescription = intent.getStringExtra("desc");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Use the data sent from MainActivity:
if (mLatLng != null) {
// Add a marker for location/description sent from MainActivity
mMap.addMarker(new MarkerOptions().position(mLatLng).title(mDescription));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
}
}
}
In addition to first answer, the mMap object is null because it is yet to be instantiated and onMapReady callback has not yet been called.
take a look at the docs for OnMapReadyCallback interface https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback

Add marker on long press in Google Maps API v3

How to implement adding a marker on long press in Google Maps API v3 for android? There are answers about this in Stack Overflow itself but they are for Google Maps API v2.
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advertiser_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
//for searching for a location
public void onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
As #tyczj pointed out, there's no v3 for Android Google Maps API, so you are probably using v2.
That said, to accomplish what you want, call setOnMapLongClickListener in your mMap object, and add the marker as you want inside onMapLongClick. You should do this in the onMapReady method:
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
}
});
}
// EDIT:
If you want to keep only one marker present at a time, you should declare your marker in the global activity's scope, and then, in onMapLongClick, if marker already exists, instead of creating a new marker, just update it's position:
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
// Declare marker globally
Marker myMarker;
...
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
// First check if myMarker is null
if (myMarker == null) {
// Marker was not set yet. Add marker:
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
} else {
// Marker already exists, just update it's position
myMarker.setPosition(latLng);
}
}
});
}
}
If done like this, remember to always check if your marker is not NULL before manipulating it in your code.
Hey guys if you all are using mMap.clear() then remove it . Mostly it work just fine.

I want to replace a Marker from a map when user visits that location ( gets near a Marker )

First, I plot a Marker like this:
public void addMarker(String title,String lat,String Lng,int id,String address,int f)
{
marker= mMap.addMarker(new MarkerOptions().snippet(title)
.title(title+", "+address)
.position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
.icon(BitmapDescriptorFactory.fromResource(id)));
LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
mMap.animateCamera(yourLocation);
mMarkerArray.add(marker);
}
After that I am trying to replace the Marker with another icon when ever I reached at any existing Location
#Override
public void onLocationChanged(Location location)
{
Log.d("latitude_main", "onlocation???");
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);
current_lat= String.valueOf(latitude);
current_lng= String.valueOf(longitude);
Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );
for(int i=0;i<salesmanlocationArrayList.size();i++)
{
if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
{
mMap.addMarker(new MarkerOptions()
.snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
.position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
}
mapFragment.getMapAsync(this);
}
}
I want to remove the marker from the map when the user visits that location.
You can simply define one OnMyLocationChangeListener class that performs your tasks, and set it on your GoogleMap instance, this way you can use it whenever you want in your application.
Step 1 - define your listener
public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {
List<Marker> markerList;
int MY_DISTANCE;
GoogleMap mMap;
public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
{
this.markerList = markerList;
this.MY_DISTANCE = meters;
this.mMap = mMap;
}
#Override
public void onMyLocationChange(Location location) {
// your code/logic
//...
Location myNewLocation = location;
Location someMarkerLocation = new Location("some location");
//for each marker on your list
//check if you are close to it
for (Marker m : markerList) {
LatLng markerPosition = m.getPosition();
someMarkerLocation.setLatitude(markerPosition.latitude);
someMarkerLocation.setLongitude(markerPosition.longitude);
if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {
//remove marker
m.remove();
//or if you still want to use it later
//m.setVisible(false);
// add your new marker
//mMap.addMarker(new MarkerOptions().icon()....);
}
}
}
}
After defining your class you just set the listener on your map on your fragment or activity code =)
Step 2 - instanciate the listener and set it
MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);
UPDATE to answer your question in the comments:
You should initialize mMap before using it, take a look at this piece of code from this Stackoverflow question
public class MapPane extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
don't forget your activity should implement the OnMapReadyCallback interface so the onMapReady method is called
you can use the map only after it is ready
Hope this helps!

Categories

Resources