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
Related
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));
}
}
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.
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!
I'm try to use startActivity, but it doesn't work.
This is my code:
setOnMarkerClickListener:
try like this...
public class MarkerDemoActivity extends Activity or FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
..............
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
Intent intent=new Intent(MarkerDemoActivity.this,AnotherActivity.class);
startActivity();
}
}
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng chennai = new LatLng(12.9671, 80.2593);
mMap.addMarker(new MarkerOptions().position(chennai).title("Chennai"));
LatLng perungudi = new LatLng(12.97, 80.25);
mMap.addMarker(new MarkerOptions().position(perungudi).title("Perungudi"));
LatLng pallikarnai = new LatLng(12.9377, 80.2154);
mMap.addMarker(new MarkerOptions().position(pallikarnai).title("Pallikarnai"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chennai,12));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.getTitle().equals("Chennai")){
Intent intent = new Intent(MapsActivity.this, LoginActivity.class);
startActivity(intent);
return false;
}
});
}
I want to know how i can set "OnClick" for a marker on google maps in android.
Example: User touch the marker and My app detect this.
My marker
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Try with OnMarkerClickListener, like following:
map.setOnMarkerClickListener(new OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker arg0) {
// do your stuff
return true;
}});
implement OnMarkerClickListener
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
myMarker = googleMap.addMarker();
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Example
this.googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
Log.d(TAG, arg0.getTitle() + " | " + arg0.getPosition().toString());
return false;
}
});