I am developing an android app in which i have to show map view i have done it once in a previous app but the key i used in the previous is not working int his app .
It is just showing a pin in the application with blank screen.
Do i have to use a different Map key for each project , If not Kindly help me how can i use my previous Key in this.
and also I tried generating a new key but gave the the same key back .
Here is the code i used
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
btn=(Button)findViewById(R.id.mapbtn);
str1=getIntent().getStringExtra("LATITUDE");
str2=getIntent().getStringExtra("LONGITUDE");
mapView = (MapView)findViewById(R.id.mapView1);
//View zoomView = mapView.getZoomControls();
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mc = mapView.getController();
btn.setOnClickListener(this);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String coordinates[] = {str1, str2};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
//mp.equals(o);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
Thanks....
amandeep there are two kinds of key for maps debug key and release key
debug key is for your system on which you are developping your application
that can be used for every application and the seconde one is release key that is different for every application and for each application you have to create new one
http://code.google.com/android/add-ons/google-apis/mapkey.html
Difference between Debug and Signing key
Map Key is Only restricted to a Single Machine as it includes Java libraries, JDK and all, so debug.keystore is also a single file and by this you will always get a UNIQUE Map Key.
You will have to Generatea new Map Key only when you Shift your Machine(CPU) or reinstall your OS.
Another advice to Just remove code of Overlay and just Check it again is it working fine. Or there is a problem in Overlay Class. Clean and Rebuild the Application.
Hope you have given these permissions also.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Are you trying to run the app on emulator? if yes Then use the default key , I f you are signing out the apk then running on a device use the same key to sign as you used for the last app and use the generated google map api key .
Are you getting grids as output ??
Two possible reasons key or internet(internet over proxy)
Related
I wonder if there is a chance to set a final Overlay in Google Maps. I wanna set my cross hairs this way, so that it isn't moving while zooming.
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
initData();
mapView = (MapView) findViewById(R.id.mapView);
mapView.setSatellite(true);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
controller = mapView.getController();
controller.animateTo(coordinate);
controller.setZoom(18);
// TODO:
CrossHairsOverlay chOverlay = new CrossHairsOverlay();
// Pin
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(coordinate);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
listOfOverlays.add(chOverlay);
}
Cross hairs:
public class CrossHairsOverlay extends Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point centerPoint = projection.toPixels(mapView.getMapCenter(), null);
Bitmap crossHairs = BitmapFactory.decodeResource(getResources(), R.drawable.cross_hair);
canvas.drawBitmap(crossHairs, centerPoint.x - (int)(27 * dpi + 0.5f), centerPoint.y - (int)(27 * dpi + 0.5f), new Paint());
return true;
}
}
If I use it this way, it recenters after zooming is finished. Left/right draging works correct. I don't really wanna set a "ZoomListener" and redraw all the time. I hope there is a bether solution to do this.
Thanks
Forgive me for not fully understanding your question...
But, to draw to a simple point no need to use the projection, just draw your bitmap strictly to the center of the mapview.
ie...
// make the coordinated constant also.
canvas.drawBitam(bitmap, center.x-(imageWidth/2), center.y-(imageHeight/2),new Paint());
where center.x and center.y are just equal to
Point center = new Point(canvas.getWidth()/2,canvas.getHeight()/2);
Also dont reload your bitmap on every onDraw!, memory leak ftl ;).
I am working on GoogleMap with MapView.Part of my project i am touching on the map and i am adding pin.My question is i want to remove pin which can added before. How can i do that i want to give Geopoint to the function.
Can anybody give me suggestion?
This is my code:`
public void AddPoint(Drawable drawable, MapView mapView, MotionEvent motionEvent) {
p = mapView.getProjection().fromPixels(
(int) motionEvent.getX(),
(int) motionEvent.getY()-50);
final MapController mc = mapView.getController();
mc.setZoom(16);
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
itemizedOverlay.addOverlay(new CustomOverlayItem(p,"","",""));
mapView.getOverlays().add(itemizedOverlay);
mc.animateTo(p);
mapView.invalidate();
}`
mapView.getOverlays().clear();
Call marker.remove(). To get the reference to the marker, you need to save the markers to a List when you create them, and then you can iterate over the List to find the ones you want.
I am trying to show the users current location with the default blue dot in android. In my maps page I also have a layout that shows different points of interest. Im having trouble figuring out what to put for some of the variables and was wondering if someone could help me out.
This is what I'm using so far to show my location.
Location location = locationManager
.getLastKnownLocation(bestProvider);
try {
GeoPoint myPoint2 = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
newoverlay.drawMyLocation(null, mapView, location, myPoint2,
1000);
mapOverlays.add(newoverlay);
} catch (NullPointerException e) {
GeoPoint myPoint2 = new GeoPoint((int) (-1 * 1E6),
(int) (-1 * 1E6));
**newoverlay.drawMyLocation(null, mapView, location, myPoint2,
1000);**
mapOverlays.add(newoverlay);
}
I'm not sure what to put as the Canvas so I placed it with null so that it would compile. I'm using the location from a location Manager and I have my geopoint from the location variable. I'm also unsure what the "when" parameter is supposed to be.
I was also wondering how the blue bubble knows to move with the person, does the picture update every x milliseconds depending on the "when" parameter?
So far the app isn't crashing, but it is also not showing the blue dot at any location.
I'm sure I just need help with finding what the canvas parameter should be.
Thanks
try this way in your map activity
class CurOverlay extends Overlay {
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
#Override
public boolean draw(Canvas canvas, MapView curmapView, boolean shadow,
long when) {
super.draw(canvas, curmapView, shadow);
// convert point to pixels
Point screenPts = new Point();
curmapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pinsource);
canvas.drawBitmap(bmp, screenPts.x - 28, screenPts.y - 48, null);
return true;
}
}
i hope this will work for you.
I have an android app that takes an array from a website
{"d":[{"latitude":-1.0,"longitude":-1.0,"time":"07:14 PM 01/18/2038","most_recent":"1","user_id":"4e3f2c6659f25a0f8400000b"},{"latitude":-1.0,"longitude":-1.0,"time":"07:14 PM 01/18/2038","most_recent":"0","user_id":"4e3f2c6659f25a0f8400000b"},{"latitude":-1.0,"longitude":-1.0,"time":"07:14 PM 01/18/2038","most_recent":"0","user_id":"4e3f2c6659f25a0f8400000b"},{"latitude":34.0608,"longitude":-118.454,"time":"07:14 PM 01/18/2038","most_recent":"1","user_id":"4e3da65e59f25a3956000005"},{"latitude":34.0608,"longitude":-118.454,"time":"07:14 PM 01/18/2038","most_recent":"0","user_id":"4e3da65e59f25a3956000005"},{"latitude":34.0608,"longitude":-118.454,"time":"07:14 PM 01/18/2038","most_recent":"0","user_id":"4e3da65e59f25a3956000005"}]}
as you can see each item has a latitude and longitude component as well as an id.
How do I modify this program to show either a green pin R.drawable.greenpin or a blue pin R.drawable.bluepin for "user_id":"4e3f2c6659f25a0f8400000b" or "user_id":"4e3da65e59f25a3956000005" for each of the items in the array ( should be three blue pins and three green pins at these locations)
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas,mapView,shadow);
Point screenPts=new Point();
mapView.getProjection().toPixels(p,screenPts);
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.greenpin);
canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);
return true;
}
}
//to add the location marker
MapOverlay mapOverlay=new MapOverlay();
List<Overlay> listOfOVerlays=mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
The method that I have confuses me a bit because it draws the point based on the screen point. I would rather add these points by inserting the pins by the longitude and latitude of the arrays.
I don't understanding what u want but in my case this code work for me, for put marker in appropriate lat,long address.
myPoint = new GeoPoint(((int) LATITUDE_Add) * 1E6),(int)LONGITUDE_Add) * 1E6));
drawable = getResources().getDrawable(R.drawable.marker_blue);
itemizedoverlay = new MapOverLay(drawable, MapPage.this, mapview);
OverlayItem overlayitem = new OverlayItem(myPoint, address, "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapview.getController().animateTo(myPoint);
Thnx.
I want to do the following and am kind of stuck on these for a few days:
I was trying to draw polylines (I have encoded polylines, but have managed to decode those) that move when I move the map.
The only solution that I found was for Geopoints to be transformed into screen coordinates... which won't move if I move the map.
I used HelloItemizedOverlay to add about 150 markers and it gets very very slow.
Any idea what to do? I was thinking about threads (handler).
I was looking for some sort of a timer function that executes a given function periodically, say, every 1 minute or so.
I was also looking for ways to clear the Google map from all the markers/lines, etc.
Answers given below :
1) Here's a solution that I used :
/** Called when the activity is first created. */
private List<Overlay> mapOverlays;
private Projection projection;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
GeoPoint gP1 = new GeoPoint(19240000,-99120000);
GeoPoint gP2 = new GeoPoint(37423157, -122085008);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
Projection projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
courtesy: Drawing a line/path on Google Maps
2) Here's what worked for me :
createMarkers()
{
for(elem:bigList)
{
GeoPoint geoPoint = new GeoPoint((int)(elem.getLat()*1000000), (int) (elem.getLon()*1000000));
OverlayItem overlayItem = new OverlayItem(geoPoint, elem.getName(), elem.getData());
itemizedOverlay.addOverlay(overlayItem);
}
itemizedOverlay.populateNow();
mapOverlays.add(itemizedOverlay); //outside of for loop
}
and in MyOverlay:
public void addOverlay(OverlayItem overlay)
{
m_overlays.add(overlay);
}
public void populateNow()
{
populate();
}
courtesy: stackoverflow.com unknown link
3) The best way is to use a timer class. A very detailed description of the timer class and how to use it is given at this link :
http://life.csu.edu.au/java-tut/essential/threads/timer.html
4) I used this code :
if(!mapOverlays.isEmpty())
{
mapOverlays.clear();
mapView.invalidate();
}
Hope these answers help atleast one other person. Thanks.
I have the same problem. We are developing an iphone app and an android app at the same time. I have 2500 + map overlays. No problem on iphone; a huge performance hit on android when calling populate() after adding all overlays. (Of course, my first try was to call populate() every time after adding an overlay; a typical mistake due to google's tutorial. Now I am calling populate() just once, after all 2500+ overlays have been added to the ItemizedOverlay.)
So the single populate() call takes over 40 seconds to complete on an htc hero device. I had to put in a busy indicator; no progress bar is possible because we cannot get any information about the progress of populate().
I tried another solution: not use ItemizedOverlay but add overlays by hand, subclassing Overlay. Result: indeed it is much faster to add all those overlays to the map; however, the map becomes unusable due to constant calling of the draw() method on each overlay. In my draw method, I tried to optimize as much as possible; I do not create a bitmap every time. My draw() method looks like this:
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
// our marker bitmap already includes shadow.
// centerPoint is the geopoint where we need to put the marker.
if (!shadow) {
Point point = new Point();
mapView.getProjection().toPixels(centerPoint, point);
canvas.drawBitmap(markerBitmap, point.x, point.y, null);
}
}
Here markerBitmap is precomputed. I don't know what else I could optimize. Is there some kind of populate() call required if we are not using ItemizedOverlay??? I could not find any good answers for that.
Right now I resort to caching the ItemizedOverlay once it has been created in a background thread. This way at least the user does not have to wait every time.
For #2, I don't think you solved anything there. Your hard-to-read code is showing how to put markers on overlay and then, how to add that overlay to the map. That's exactly how I do it. I have map with around 300 hotels and it takes around 5 seconds for Android on my Nexus One to create markers. The whole thing is running inside thread and I guess I will have to do some sort of progress bar to let user know what's going on.
I am working on app that already exists on iPhone and it seems iPhone doesn't have any issues to almost instantaneously draw these 300+ markers. I'll have hard time to explain existence of progress bar to my bosses.
If anybody have idea how to optimize this process... I will be grateful.
Here is my code:
...
for (int m = 0; m < ArrList.size(); m++) {
tName = ArrList.get(m).get("name").toString();
tId = ArrList.get(m).get("id").toString();
tLat = ArrList.get(m).get("lat").toString();;
tLng = ArrList.get(m).get("lng").toString();;
try {
lat = Double.parseDouble(tLat);
lng = Double.parseDouble(tLng);
p1 = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
OverlayItem overlayitem = new OverlayItem(p1, tName, tId);
itemizedoverlay.addOverlay(overlayitem);
} catch (NumberFormatException e) {
Log.d(TAG, "NumberFormatException" + e);
}
}
I know I could save some time by avoiding this String > Double conversion, but I don't feel that would give me significant saving.. or it would?
For your 4th question.... simply use the mapOverlays.clear(); method and all the previous markers will be vanished.
code:
if(!mapOverlays.isEmpty()) {
mapOverlays.clear();
mapView.invalidate();
}
Multiple number of drawable objects can be added to a single Overlay which can then be added to the map. Hence, drawing x number of overlay's for x number of objects wouldnt be necessary unless the objects are of different types. Code snippet..
..
Here, CustomPinPoint is my class which extends ItemizedOverlay<OverlayItem>
CustomPinPoint customPinPoint = new CustomPinPoint(drawable, Main.this);
OverlayItem tempOverLayItem = new OverlayItem(
touchedPoint, "PinPoint", "PintPoint 2"); //Point One
customPinPoint.insertPinPoint(tempOverLayItem);
tempOverLayItem = new OverlayItem(new GeoPoint(
(int)(-27.34498 * 1E6), (int)(153.00724 * 1E6)), "PinPoint",
"PintPoint 2"); //Point Two
customPinPoint.insertPinPoint(tempOverLayItem);
overlayList.add(customPinPoint); //Overlay added only once