I'm trying to get and set my GoogleMap variable using the the following code:
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();
//MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.myMapView);
//mMap = fm.getMap();
Log.e("RideTracking", "Google Map VALUE:"+mMap);
if (mMap != null) {
proxy.setProjection(mMap.getProjection());
}
Here is my XML for this part of the app:
<fragment
android:id="#+id/myMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
For some reason the value of mMap is null and I'm not sure why it is returning null since I have this line of code:
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();
Here is what I get for logcat output:
05-13 14:09:08.553: E/RideTracking(6415): Google Map VALUE:null
Can anybody offer any advice on why the of mMap is null? Any help would be appreciated.
If try putting your code in the method onActivityCreated(). This could make sure your map is ready.
I was having the same problem with the mMap being NULL.
Try adding the following lines of code under the mMap call.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map = mapFragment.getMap();
It fixed my problem with the NULL issue.
Try looking into the answer in this post
NullPointerException whilst trying to add polyline/marker to a GoogleMap
That is because you are not able to connect to google maps api, have you obtained the key from google and added it to your manifest? See the links below
https://developers.google.com/maps/documentation/android/start
http://www.vogella.com/articles/AndroidGoogleMaps/article.html
Follow these instructions and if it still not works post your manifest and the code i will help you, i have worked on maps
I had the same problem and then I found that I forgot to write line
setContentView(R.layout.activity_direction);
Then after my map is working fine. So please ensure that you have set you layout before declaring map.
Related
I posted the code snippet below as I'm unable to find the command setUpMapIfNeeded();. When I type the necessary command, I get red lines everywhere. Can anyone help me out with this code? Also, I'm not getting any marker in my emulator.
I'm open to suggestions, please help me out. The min sdk version that I'm using is 17, and the android studio version is 2.1. I'm using the Googlemaps activity. I still don't understand why I'm not getting the setUpMapIfNeeded command and also the rest of the inbuilt code snippet.
Code screenshot
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#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));
}
}
Also after trying the missing code I'm unable to get any marker.
Whereas, wherever I have search online I'm getting this tutorial.
If you are aware of any other code, do let me know.
When you use MapsActivty, there is no need for any extra code. All the neccessary code is generated by Android Studio itself. If you look at the tutorial, the code inside the setUpMapIfNeeded() is same as the code you have in your onCreate(). When you add the Google MapsActivty, all you need to show the map is the Google Maps API key. Paste the APi key in your "google_maps_api.xml"(inside values folder) and you are done. Run the application and you will get the map in your Activity.
Steps for acquiring the Google Maps API key is given in the google_maps_api.xml in comments.
Also if are adding your own markers, then use the following code
public void setMarkerOnMap(String name, LatLng l) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(l);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(name);
// Placing a marker on the touched position
mMap.addMarker(markerOptions);
}
Call this function like this :
LatLng loc=new LatLng(latitude,longitude);
String str_placeName="Place Name";
setMarkerOnMap(str_placeName,loc);
You need to call setUpMapIfNeeded() in onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
Check this tutorial and SO question on how to add markers.
I'm fairly new to Android programming and I'm a bit stuck. I have a map set up and now I want to add the blue flashing dot to the application to show where the user is on the map.
How do I do this?
Just try implementing this code, hope this works fine for you.
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
If it does not work for you, try keep in touch with me.
I am trying to add current location mark to my android Google map application in Android Studio.
My code:
private void setUpMap() {
Location location = null;
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
Marker marker;
marker = mMap.addMarker(new MarkerOptions().position(new LatLng((location.getLatitude()), location.getLongitude())).title("Marker"));}
It causes my application to crash. Underneath there are mistakes that appeared while running my application:
http://postimg.org/image/qrxdo9dl5/full/
(I used postimage to upload a larger size screen-shot)
Before I modified my code, I had a working code set to position 0,0:
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
Then I tried to change location '0,0' to read current latitude and longitude and show it on my Google map. I changed my code based on this post: How to get the current location in Google Maps Android API v2?
I am a java beginner and any help is much appreciated. Thank you very much for any help.
You are getting a NullPointerException because your Location object is set to null. In the linked answer the location object comes from the location callback.
public void onMyLocationChange(Location location) // location object here
You can easily get the Location from your map reference like so
Location location = mMap.getMyLocation();
However this method is deprecated and using the location API is encouraged. To get location from the API you can follow this guide:
https://developer.android.com/training/location/retrieve-current.html
You build a GoogleApiClient, connect it, and then use it to get a location.
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Trying to implement google map API in my android project and getting the warning error that is under my java code
GoogleMap gMap;
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
LatLng Jamaica = new LatLng(18.1155173, -77.2760026);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Jamaica, 13));
gMap.addMarker(new MarkerOptions()
.title("Jamaica")
.snippet("The greatest country in the world")
.position(Jamaica));
gMap.setMyLocationEnabled(true);
Logcat:
03-18 12:45:00.140: W/ActivityThread(10477): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
Run `cmd` and genereate a new "SHA1" key.
Browse to https://code.google.com/apis/console/?noredirect `API Console` page.
Replace the old "ShA1" with the new "sha1" key.
Then replace your new map key.
Your problem solved. I also had this problem. (I only changed my map key)
Did you add
<uses-library android:name="com.google.android.maps" />
to your manifest under the application tag?
This is my first post here, so I hope I'll explain my problem clear enough:
I'm extending the FragmentActivity class to create a HeatMapActivity. In this activity I use a GoogleMap object, which I obtain as follows:
// Try to obtain the map from the SupportMapFragment.
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.heatmap)).getMap();
Now, after doing a null-Check etc., I'm adding a TileOverlay in the setUpMap() method, as shown below:
// Configure and add overlay
tileProvider = new HeatMapTileProvider(map);
map.addTileOverlay(new TileOverlayOptions()
.tileProvider(tileProvider));
Ok, so far no problem at all. I implemented the TileProvider interface to provide the tiles for the HeatMap overlay.
I want to use the Projection object for converting latitude/longuitude to and from pixels on the screen. The Projection object should be returned by the getProjection() method of the GoogleMap class.
So now here's the problem: If I use the following code in the HeatMapActivity class, everything is fine:
System.err.println("Test1: " + map.getProjection().fromScreenLocation(new Point(50, 50)));
But if I forward the GoogleMap object to the TileProvider, respectively to the renderer I use, the code is not working anymore:
public byte[] render(int x, int y, int zoom, GoogleMap map) {
...
System.err.println("Test2: " + map);
System.err.println("Test3: " + map.getProjection().fromScreenLocation(new Point(50, 50)));
...
}
I definitely know, that the map parameter is not null (see Test 2), but the code just seems to block when I call getProjection(). There is no error coming up and if I don't use the Projection object, everything else is working fine. I don't have any clue, why I should not be allowed to call getProjection() from another class than the activity...
I am using the ADT bundle for Windows (version x86_64-20130729), Android SDK version from 8 to 17, testing on Android 2.3.5 at the moment. The map is displayed, the needed libraries are included and the permissions are set in the android manifest.
Any ideas? Any help or hints are appreciated.
Seb
The issue here is that getTile (and your render method) are called outside UI thread.
This will cause exception, which you won't see, because it is probably silently handled by the code calling getTile. This is really bad it doesn't crash, so someone could post an issue on gmaps api issues.
Try to wrap call to getProjection in try-catch to see it.
Because you should not interact with GoogleMap outside of main thread, you will have to find another way to achieve, what you want to achieve...
Anyway, I can imagine no good reason for a need to call getProjection in getTile. Maybe another question describing your real problem?