I have the following code and, when I press a menu button I would like to reposition the same marker and the camera but I do not have idea how to do it, I've read oficial documentation but I haven't found info. I'm using repositionMarker() method to do it, but Im not sure what to do. Thanks
public class Init extends AppCompatActivity implements OnMapReadyCallback
{
private MapFragment map;
static final LatLng PERTH = new LatLng(19.0436, -98.1981);
private CameraUpdate camaraloc, camaraloczoom;
private Marker markerloc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (map != null)
{
map.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap map)
{
map.setMyLocationEnabled(true);
camaraloc = CameraUpdateFactory.newLatLng(PERTH);
camaraloczoom = CameraUpdateFactory.zoomTo(8);
map.moveCamera(camaraloc);
map.animateCamera(camaraloczoom);
markerloc = map.addMarker(new MarkerOptions().position(PERTH)
.title("city").snippet("esp").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_inicio, menu);
return true;
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_settings:
Toast.makeText(this, "name marker: "+markerloc.getTitle(), Toast.LENGTH_SHORT).show();
reposicionMarker();
return true;
}
return super.onOptionsItemSelected(item);
}
public void repositionMarker()
{
camaraloc = CameraUpdateFactory.newLatLng(new LatLng(16.65, -91.8658));
camaraloczoom = CameraUpdateFactory.zoomTo(15);
if ( markerloc != null )
markerloc.remove();
markerloc.setPosition(new LatLng(16.65, -91.8658));
// here is where I dont know what to do
}
}
Calling setLocation on the Marker should work, for changing its location. What seems to be your problem is that you are actually removing the marker right before you set the new location.
Related
I am trying to have a simpler implementation of Google Map in my Android App. I am able to launch Google Map but I am unable to show multiple markers there. If I keep the latitude and longitude values and name of the places hardcoded then things work, but dynamically with variables and String values I am unable to show them.
I have also gone through this link suggested by many over here. But what I actually want is a simpler implementation of it.
Below is the code that I have used in my Main Activity:
public class Map_Activity_With_Fragment extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap googleMap;
String Origin, Destination, Start_Date, Num_Adult, Num_Infant, Origin_Map, Destination_Map;
Context context;
List<Address> Addr_Origin, Addr_Dest;
double latitude_origin, longitude_origin, latitude_destination, longitude_destination;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Origin = extras.getString(MainActivity.ORIGIN);
Destination = extras.getString(MainActivity.DESTINATION);
Start_Date = extras.getString(MainActivity.START_DATE);
Num_Adult = extras.getString(MainActivity.ADULT);
Num_Infant = extras.getString(MainActivity.INFANT);
Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
context = Map_Activity_With_Fragment.this;
setTitle("Location Map");
setContentView(R.layout.activity_map__with__fragment);
try
{
initializeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_map__with__fragment, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
AddMarkers(googleMap);
}
#Override
protected void onResume() {
super.onResume();
initializeMap();
}
private void initializeMap()
{
if (googleMap == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
private void AddMarkers(GoogleMap googleMap)
{
try {
Geocoder geocoder = new Geocoder(context);
Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
if (Addr_Origin.size() > 0) {
latitude_origin = Addr_Origin.get(0).getLatitude();
longitude_origin = Addr_Origin.get(0).getLongitude();
}
if (Addr_Dest.size() > 0) {
latitude_destination = Addr_Dest.get(0).getLatitude();
longitude_destination = Addr_Dest.get(0).getLongitude();
}
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map));
Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please help me out!
Thanks in advance!
Ok, so I am going to update my own question.
I was testing these code changes on a device of API level 16, and hence the markers and polylines were not getting rendered correctly. Then I tested this same code on a device of API level 19, and it worked perfectly fine. I did not modify any part of code for this. Things are working now. :)
I have an Android app for searching flights. I want to implement a Google Map where the Origin and the Destination will be shown in the map (with the help of some markers) to the user based on his/her inputs. But before I start anything, I need a help in the overall understanding.
Which API do I need? Google Maps API or Google Maps Direction API? I don't want to show any direction, only the places marked.
Is there any HTTP request for Google Maps API? Or how do I dynamically show the places in the map? I will only have the name of the places.
I would really appreciate if somebody can guide me through the initial steps of this implementation.
EDITED:
Below is the Main Activity that I am using:
public class Map_Activity_With_Fragment extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap googleMap;
String Origin, Destination, Start_Date, Num_Adult, Num_Infant, Origin_Map, Destination_Map;
Context context;
List<Address> Addr_Origin, Addr_Dest;
double latitude_origin, longitude_origin, latitude_destination, longitude_destination;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Origin = extras.getString(MainActivity.ORIGIN);
Destination = extras.getString(MainActivity.DESTINATION);
Start_Date = extras.getString(MainActivity.START_DATE);
Num_Adult = extras.getString(MainActivity.ADULT);
Num_Infant = extras.getString(MainActivity.INFANT);
Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
context = Map_Activity_With_Fragment.this;
setTitle("Location Map");
setContentView(R.layout.activity_map__with__fragment);
try
{
initializeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_map__with__fragment, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
AddMarkers(googleMap);
}
#Override
protected void onResume() {
super.onResume();
initializeMap();
}
private void initializeMap()
{
if (googleMap == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
private void AddMarkers(GoogleMap googleMap)
{
try {
Geocoder geocoder = new Geocoder(context);
Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
if (Addr_Origin.size() > 0) {
latitude_origin = Addr_Origin.get(0).getLatitude();
longitude_origin = Addr_Origin.get(0).getLongitude();
}
if (Addr_Dest.size() > 0) {
latitude_destination = Addr_Dest.get(0).getLatitude();
longitude_destination = Addr_Dest.get(0).getLongitude();
}
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map));
Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Google Maps API should be enough.
If you have location you can adds marker for each place https://developers.google.com/maps/documentation/android-api/marker
Get cooridnates:
How to get coordinates of an address in android
I want to pin a new mark on my map but method onMapLongClick didn work.
My code is below. I have my map inside a fragment.
public class Fragment_maps extends android.support.v4.app.Fragment
implements OnMapLongClickListener, OnMapClickListener{
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_maps, container,
false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
//activate menu button
setHasOptionsMenu(true);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
//get the map
googleMap = mMapView.getMap();
// Detect location
googleMap.setMyLocationEnabled(true);
//googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Turns traffic layer on
googleMap.setTrafficEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Turns on 3D buildings
googleMap.setBuildingsEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
// latitude and longitude
double latitude = 40.639350;
double longitude = 22.944607;
// create marker
MarkerOptions thessaloniki = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Thessaloniki");
//----------------
MarkerOptions LeykosPyrgos = new MarkerOptions().position(new LatLng(40.626401, 22.948352)).title("Leykos Pyrgos");
// Changing marker icon
thessaloniki.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
//----------------
LeykosPyrgos.icon(BitmapDescriptorFactory .fromResource(R.drawable.whitetower));
// adding marker
googleMap.addMarker(thessaloniki);
CameraPosition cameraPositionThess = new CameraPosition.Builder() .target(new LatLng(40.639350, 22.944607)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionThess));
//----------------
googleMap.addMarker(LeykosPyrgos);
CameraPosition cameraPositionLeykosPyrgos = new CameraPosition.Builder() .target(new LatLng(40.626401, 22.948352)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPositionLeykosPyrgos));
// Perform any camera updates here
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_maps, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected","yes");
switch (item.getItemId()) {
case R.id.HYBRID:
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);;
return true;
case R.id.SATELLITE:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return true;
case R.id.TERRAIN:
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
return true;
case R.id.NORMAL:
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onMapClick(LatLng point) {
Toast.makeText(getActivity(),point.toString(),Toast.LENGTH_SHORT).show();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
#Override
public void onMapLongClick(LatLng point) {
googleMap.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
As you can see I have both onMapClick and onMapLongClick methods but when I click on map nothing happens. I also have implement OnMapLongClickListener and OnMapClickListener. What am I doing wrong?
Thanks in advance.
Your class implements the listener but you don't set it:
googleMap.setOnMapLongClickListener(this);
same with the normal click listener:
googleMap.setOnMapClickListener(this);
I HAVE A PROBLEM WITH googleMap.setMapType IN MY ANDROID APP.WHEN I SELECT googleMap.setMapType IN MY MAIN MENU THE COMAND DO NOT HAVE EFFECT ON GOOGLE MAP,WHY?
THI IS MY CODE :
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
private static final LatLng casa = new LatLng(48.411709,10.971354);
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(casa)
.title("... CASA ...").snippet("LA MIA CASA.")
.draggable(true));
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(casa, 5));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.mapsat:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
HAVE YOU A SOLUTION?THANKS!!
below is my code which display map of all world i want when application start is show specific city not world map i follow this tutorial http://android-er.blogspot.com/2013/01/google-maps-android-api-v2-example-draw.html every thing work fine but i want when app start first time is display specific city map not all global map i dont want show globap map like this image http://1.bp.blogspot.com/-dwjqTWCdONg/UObUk1syAFI/AAAAAAAAGy8/S43YMKuXZRc/s1600/screen_MapsAPIv2_Polygon.png on app start i want specific city map like newyork for example.
public class MainActivity extends Activity
implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
TextView tvLocInfo;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocInfo = (TextView)findViewById(R.id.locinfo);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
myMap.setOnMarkerClickListener(this);
markerClicked = false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
RQS_GooglePlayServices);
}
}
#Override
public void onMapClick(LatLng point) {
tvLocInfo.setText(point.toString());
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
markerClicked = false;
}
#Override
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added#" + point.toString());
myMap.addMarker(new
MarkerOptions().position(point).title(point.toString()));
markerClicked = false;
}
#Override
public boolean onMarkerClick(Marker marker) {
if(markerClicked){
if(polygon != null){
polygon.remove();
polygon = null;
}
polygonOptions.add(marker.getPosition());
polygonOptions.strokeColor(Color.RED);
polygonOptions.fillColor(Color.BLUE);
polygon = myMap.addPolygon(polygonOptions);
}else{
if(polygon != null){
polygon.remove();
polygon = null;
}
polygonOptions = new PolygonOptions().add(marker.getPosition());
markerClicked = true;
}
return true;
}
}
add this code to your onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
// define point to center on
LatLng origin = new LatLng(40.67, -73.94);
CameraUpdate panToOrigin = CameraUpdateFactory.newLatLng(origin);
myMap.moveCamera(panToOrigin);
// set zoom level with animation
myMap.animateCamera(CameraUpdateFactory.zoomTo(14), 400, null);
}
After the onClikListener are setted (into onCreate), put this code
LatLng NewYork= new LatLng(40.714623,-74.006605);
CameraPosition camPos = new CameraPosition.Builder().target(NewYork).zoom(14).build();
CameraUpdate cam = CameraUpdateFactory.newCameraPosition(camPos);
myMap.animateCamera(cam);
It will animate your map and move the camera to NY