Unable to create Google map fragment - java

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?

Related

Get current location with network provider on osmdroid

In my android app I am using the osmdroid framework. In the framework I am using the MyLocationNewOverlay() which contains a method to show the current location on the map.
My problem
The LocationListener in the framework seems not to use the network provider and only wants to locate me with GPS (which works fine, but only if I am outside).
Is there a standard way to use a LocationProvider that also works with the network provider if gps is not available?
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Pretty standard to init my location overlay:
private void initMyLocationNewOverlay() {
myLocationNewOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(context), mapView);
myLocationNewOverlay.enableMyLocation();
mapView.getOverlays().add(myLocationNewOverlay);
}
Thanks in advance!
The problem is that the GpsMyLocationProvider only adds the GPS_Provider to its sources (you can see that in the constructor). To add the network provider use the addLocationSource as following.
private void initMyLocationNewOverlay() {
GpsMyLocationProvider provider = new GpsMyLocationProvider(context);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
myLocationNewOverlay = new MyLocationNewOverlay(provider, mapView);
myLocationNewOverlay.enableMyLocation();
mapView.getOverlays().add(myLocationNewOverlay);
}

How to show a label(String) over a marker in google maps static api?

I am working on a vehicle routing algorithm I need to show vehicles on maps with there driver name on top of the label. I have done the marker part it looks like this.
However I would like to display driver's name on top of every marker. Can you suggest me any way to do it?
My google maps static api url looks like this
https://maps.googleapis.com/maps/api/staticmap?center=26.900,75.800&zoom=11&size=400x400&markers=color:blue|label:Driver|&markers=color:green|label:Req|26.925571778374632,75.81166033197894|&markers=color:blue|label:others|26.944421737574313,75.8072312248272|26.941722318252367,75.82851178160593|26.909618922760885,75.85079885210592|26.92537722447672,75.85626510000787|26.969253598622025,75.81687986464266|26.933707124740607,75.84999920863306|26.986333832648626,75.88755301718626|26.945330532812793,75.81567681826434|26.942938812152843,75.81989078933901|&markers=color:red|label:Req|26.8983489,75.796436|
Ps: I am using java.
I think the best you can do with the Static Maps API v2 is to have different initials and colors for different markers. Like this example.
You may be able to do something with the custom icons too, but it does not seem to be working as far as I can tell.
The best practice here is to implement it using the JavaScript API v3 instead.
Is this possible with the version you're using?
#Override
public void onMapReady(GoogleMap map) {
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));
}
Here is the code in action: https://developers.google.com/maps/documentation/android/

Android GoogleMap getProjection() not working

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?

Add custom View as marker in Google Maps API v2

I've developed a class that inherits from a View class and I want it to serve as a marker on a MapFragment/MapView from a Google Maps API v2. As I recall, such thing was possible in API v1 and even now it is possible on iOS. I'm looking for something like this:
MapView map = (MapView)findViewById(R.id.mapview);
CircleTimerView myTimer = new CircleTimerView(this);
LayoutParams lp = new LayoutParams(iv.getWidth(), iv.getHeight(), geoPoint, LayoutParams.BOTTOM);
map.addView(myTimer,lp);
My custom View is animated and it is a key feature of my app.
My question is: Is it possible for a current state of Google Maps API? Or should I try to obtain API v1 key and work with something that was deprecated?
Thanks in advance for your replies.
should I try to obtain API v1 key and work with something that was deprecated?
You cannot obtain new keys for API v1.
My custom View is animated and it is a key feature of my app.
You cannot have animated Marker icon unless you repeatedly change the icon using Marker.setIcon.
Is it possible for a current state of Google Maps API?
You can use a View as icon if you draw it into a Bitmap first.
This library from Chris Broadfoot can greatly help you with that task:
https://github.com/googlemaps/android-maps-utils
See this video for what the lib can do: http://www.youtube.com/watch?v=nb2X9IjjZpM
Or the new website: http://googlemaps.github.io/android-maps-utils/
in order to customize / use your own custom maker on google maps api v2 you could write the code as follows:
in onCreate declare the marker:
userIcon = R.drawable.red;
and then i used it where you need it, in my case i used it in a method:
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(userIcon))
.snippet("Your last recorded location"));
theMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 100, null);
CameraUpdate center=
CameraUpdateFactory.newLatLng(lastLatLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
theMap.moveCamera(center);
theMap.animateCamera(zoom);
hope this helped. good luck
i had the same issue these days and you should do the switch for v2 as you can do all the same stuff from v1 easier + many new features.
MapView has been replaced with the new GoogleMap-Objecttype.
A very detailed and decent Tutorial site can be found HERE!

Google Map variable is null

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.

Categories