android: google map loading issue - android

I'm using google map in my app , the code runs on device perfectly but the map does not load ! I just see a white screen ! what's the problem ?
here is the code , please help !
public class MyMap extends MapActivity {
private MapView map;
private MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
}
/** Find and initialize the map view. */
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}
/** Start tracking the position on the map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
//overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
// Required by MapActivity
return false;
}
}

put your map api key in xml here is tutorial to get map api key

Create your google map api key and pest in maplayout.xml
android:apiKey=" your api key"
which is freely avaliable

Related

Show a panel on MapBox in Android

I use MapBox in Android and show some Points on it. Now I want to show a panel top-left of map and show some information in this panel. How can i do this? (An Example show in image in URL: https://docs.mapbox.com/help/img/directions/gljs-plugin.png , a panel like that panel show in image , the black panel on the left)
Thanks
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, YOUR_MAPBOX_ACCESS_TOKEN);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(#NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});

Why is only one Google Maps V2 marker displaying in a Fragment?

I'm using the solution here to add markers to a map in a Fragment and can't figure out why I can only add one marker.
I'm sure I'm missing something when I build the camera position, which is getting called twice in my test code. (See addLocationToMap below).
I am retrieving an ArrayList of geocoordinates on the main thread and want to loop through the list in the map tab calling addLocationToMap on each entry.
Here's my code:
public class RentalMapFragment extends Fragment {
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_map, container, false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
int level = 12;
addLocationToMap(17.385044, 78.486679, "Marker 2", level);
addLocationToMap(17.385044, 78.486671, "Marker 1", level);
// Perform any camera updates here
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385000, 78.486600)).zoom(level).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return v;
}
private void addLocationToMap(double latitude, double longitude, String markerTitle, int level) {
// Create lat/lon
LatLng latLng = new LatLng(latitude, longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(latLng).title(markerTitle);
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
#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();
}
}
It turns out that I should not be creating the camera position after placing each marker. Rather, after adding all my markers I need to choose a postion for the camera to view the markers from.
I'm updating my question to demonstrate this.

Setting a location in Map View

I'm using a map view and what I want is to mark all the ex. starbucks that near me.
but I don't have any idea how to do this.
Right now all I can do is to view my current location.
Use ItemizedOverlay to display multiple markers in android, to understand how itemized overlay works, and what it is refer following links, and tutorials:
https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/ItemizedOverlay
http://android-coding.blogspot.in/2011/06/using-itemizedoverlay-to-add-marker-on.html
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/ILuvNooYawk/
Try this
public class MyMap extends MapActivity{
MapView mapView;
MapController mapController;
/* positon */
double latitude = Your latitude;
double longitude = Your longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(8);
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
mapController.setCenter(pointRabat);
}
private int microDegres(double value) {
return (int) (value * 1000000);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
for add overlay
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
OverlayItem overlayitem = new OverlayItem(point, "My Current Location", "My Current Location");
overlay.addOverlay(overlayitem);
mapOverlays.add(overlay);
}

Android MapView

I'm trying to add an overlay for myLocation in Android. The map displays, but the overlay does not. I did get the overlay to appear using a separate class that extends ItemizedOverlay. I'm wondering of there is a way to display this individual point without creating a separate class?
Attached is the source code for the activity class.
public class WalkAbout extends MapActivity {
//for Hello_mapview
List<Overlay> mapOverlays;
Drawable drawable;
private MapView m_vwMap;
private MapController m_mapController;
private PathOverlay m_pathOverlay;
private MyLocationOverlay m_locationOverlay;
private ArrayList<GeoPoint> m_arrPathPoints;
private ArrayList<OverlayItem> m_arrPicturePoints;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLocationData();
initLayout();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private void initLocationData() {
// TODO
}
private void initLayout() {
//instantiate XML File into corresponding view objects.
//Then inflate new view from XML resource.
setContentView(R.layout.map_layout);
MapView m_vwMap = (MapView)findViewById(R.id.mapview);
m_vwMap.setBuiltInZoomControls(true);
m_vwMap.setSatellite(true);
//retrieve list of overlay objects
mapOverlays=m_vwMap.getOverlays();
//set market for overlays
drawable=this.getResources().getDrawable(R.drawable.item);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
//create OverlayItem with my location
m_locationOverlay= new MyLocationOverlay(this, m_vwMap);
//enable market to set location and direction
m_locationOverlay.enableCompass();
m_locationOverlay.enableMyLocation();
mapOverlays.add(m_locationOverlay);
}
}
You need to call invalidate() on your m_vwMap so it will redraw itself.

Why are my overlays on a mapview not shown?

I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.
Edit
Here is my source code. It should be very close to the google tutorial source code.
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;
public MapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
overlays = new ArrayList<OverlayItem>();
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
#Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
this.populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
R.drawable.androidmarker), this);
overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
mapView.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Is the source code from the google tutorial available somewhere?
The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.
I changed the first line in my constructor from:
super(defaultMarker);
to
super(boundCenterBottom(defaultMarker));
and know its working perfect.
At the same time, I have no idea how to help you directly.
Here are links to various editions of a project that definitely work with overlays, perhaps they will help.

Categories

Resources