Sponser

create your own banner at mybannermaker.com!

Thursday, April 21, 2011

Google map example in android with info window (Popup) and multiple markers and zoom controls

Through this application we can populate a google map with the info window, zoom controls and marker features. The image of the running application is shown below



First of all, we can run the application with the google api sdk only, we can use from API level 3, but the Google api sdk should be used to run the application.

Custom Linear layout is used on this application




LocationViewers.java

package com.icons.draw.view;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.icons.draw.R;

public class LocationViewers extends LinearLayout {

private MapLocationOverlay overlay;

// Known lat/long coordinates that we'll be using.
private List mapLocations;

public static MapView mapView;

public LocationViewers(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public LocationViewers(Context context) {
super(context);
init();
}

public void init() {

setOrientation(VERTICAL);
setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
String api = getResources().getString(R.string.map_api_key);
mapView = new MapView(getContext(),api);
mapView.setEnabled(true);
mapView.setClickable(true);
addView(mapView);
overlay = new MapLocationOverlay(this);
mapView.getOverlays().add(overlay);
mapView.getController().setZoom(5);
mapView.getController().setCenter(getMapLocations().get(0).getPoint());
}


public List getMapLocations() {
if (mapLocations == null) {
mapLocations = new ArrayList();
mapLocations.add(new MapLocation("Avinashi road, Coimbatore",11.0138,76.9871));
mapLocations.add(new MapLocation("Marina Beach, Chennai",13.0548,80.2830));
mapLocations.add(new MapLocation("Taj Mahal, New Delhi",28.6353,77.2250));
mapLocations.add(new MapLocation("Meenakshi Temple, Madurai",9.9195,78.1208));
}
return mapLocations;
}

public MapView getMapView() {
return mapView;
}
}



In this LocationViewers.java only we are using the MapView, then this file will be called in the map.xml


map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.icons.draw.view.LocationViewers
android:id="@+id/map_location_viewer" android:layout_width="fill_parent"
android:layout_height="fill_parent" >


</com.icons.draw.view.LocationViewers>

<ZoomControls android:id="@+id/zoomcontrols"
android:gravity="bottom"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</FrameLayout>


In the above mentioned xml file we had used the ZoomControls also, this will be given the zoom listener on the java file


DrawIcons.java

package com.icons.draw;

import com.google.android.maps.MapActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ZoomControls;
import com.icons.draw.view.LocationViewers;

public class DrawIcons extends MapActivity {
@Override
public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.map);


ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocationViewers.mapView.getController().zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocationViewers.mapView.getController().zoomOut();
}
});
}

/**
* Must let Google know that a route will not be displayed
*/
@Override
protected boolean isRouteDisplayed() {
return false;
}
}



While pressing the zoom controls plus or minus, this listener will be called and the zoom functionality will be occurs. Then we are going to see two files MapLocation.java and MapLocationOverlay.java, where in the MapLocationOverlay file we will be drawing the marker and the info window for each marker, you can get the code from the zip file below.

The main thing to run the application is adding the internet permission and google library in the AndroidManifest.xml file


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.icons.draw"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DrawIcons"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="4" />

</manifest>



The code is fulling checked so you can run the code after downloading it itself.

You can download the full source code


Download source


Have a good day.

23 comments:

  1. Hi,
    i haev downloaded your application.. and it is showing error...
    sir am getting The followowing classes could not be instaniated

    com.icons.draw.view.locationviewers

    ReplyDelete
    Replies
    1. Hi Nachi27, could you please send me the downloaded zip folder
      my email id is :- asp.amitsuri@gmail.com

      Delete
  2. Hi friend,
    Can you tell me using which avd you ran this application

    ReplyDelete
  3. Hi nachi27 i need more information, because that is our class which is not instainiated

    ReplyDelete
  4. HI
    this application working properly but we want to put name at run time. The given locations are prefixed in the map.

    ReplyDelete
  5. in the LocationViewr class MapLocationOverlay is not resolved

    ReplyDelete
  6. i am getting out of memory exception. Here is the log cat report:


    06-17 12:23:21.219: ERROR/dalvikvm-heap(3773): 731520-byte external allocation too large for this process.

    06-17 12:23:21.219: ERROR/dalvikvm(3773): Out of memory: Heap Size=8839KB, Allocated=5230KB, Bitmap Size=15056KB

    06-17 12:23:21.219: ERROR/GraphicsJNI(3773): VM won't let us allocate 731520 bytes

    06-17 12:23:21.219: DEBUG/AndroidRuntime(3773): Shutting down VM

    06-17 12:23:21.219: WARN/dalvikvm(3773): threadid=1: thread exiting with uncaught exception (group=0x400259f8)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): FATAL EXCEPTION: main

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.graphics.Bitmap.nativeCreate(Native Method)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.graphics.Bitmap.createBitmap(Bitmap.java:574)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:444)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.google.android.maps.ZoomHelper.beginZoom(ZoomHelper.java:194)
    0
    6-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.google.android.maps.MapView$2.onScaleBegin(MapView.java:371)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ScaleGestureDetector.onTouchEvent(ScaleGestureDetector.java:248)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.google.android.maps.MapView.onTouchEvent(MapView.java:646)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.View.dispatchTouchEvent(View.java:3765)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.app.Activity.dispatchTouchEvent(Activity.java:2093)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.os.Handler.dispatchMessage(Handler.java:99)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.os.Looper.loop(Looper.java:144)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at android.app.ActivityThread.main(ActivityThread.java:4937)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at java.lang.reflect.Method.invokeNative(Native Method)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at java.lang.reflect.Method.invoke(Method.java:521)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

    06-17 12:23:21.229: ERROR/AndroidRuntime(3773): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
  7. Hi,i have downloaded the code,iam getting the ballons display but i am not getting the map display in the background.Please provide the solution to me.

    ReplyDelete
  8. In the String api=""
    you have to give your google api to display the map

    ReplyDelete
  9. Hello, I have similar problem.
    I generated my key using keytool. When I list my keys, it looks like this:
    skurt@surt ~ $ keytool -list
    Enter keystore password:

    Keystore type: JKS
    Keystore provider: SUN

    Your keystore contains 1 entry

    mykey, 16.7.2011, PrivateKeyEntry,
    Certificate fingerprint (MD5): 09:96:5A:38:E8:82:A2:5F:25:9A:6F:57:16:AC:88:A3

    So, I inserted the 09: ... string to the google page and got my own api code. I've inserted the api code to you application and I still see just the grid.
    I think its really expectable, since the application do no know nothing about the certificate file...how do I solve this?

    ReplyDelete
  10. hi friend,

    i faced a problem, how to add a line in the info window,
    from this resource only display the text in one line

    ReplyDelete
  11. @Skurt the same thing happens to me, its really frustrating as I am trying to develop an app for the market....

    ReplyDelete
  12. Ok @Skurt and @VKP, i think you both are doing a mistake.

    Right, you both should have created a keystore and for that you both will be created the google api key.

    But what is the thing is, you need to check in the eclipse wheather you are using that keystore. For checking go to Windows(tab)->preferences->Android->Build

    There you will notice the Default keystore, you have give the created keystores path there, otherwise you need to get the google api key for that default keystore

    For creating keystore and google api key for map, follow this link http://android-codes-examples.blogspot.com/2011/07/creating-keystore-and-google-api-key.html

    ReplyDelete
  13. Skurt and VKP, maybe this can help :
    1. open with text editor : res > values > string.xml
    2. change
    05ux1jpKOnAwMou6Y2mVwnSsF3epEy_kC8rKARg
    with
    YOUR GOOGLE API KEY

    ReplyDelete
  14. thnk u thnk u thnk u vry mch.....:-*

    ReplyDelete
  15. Can you please tell us which Android Version is this code written for?
    It doesn't seems to be working on 2.3.3... It compiles fine, but when running I get the following error:

    09-30 21:41:20.919: ERROR/AndroidRuntime(907): java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at dalvik.system.DexFile.defineClass(Native Method)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:207)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:200)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:99)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:130)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:3683)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:507)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    09-30 21:41:20.919: ERROR/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)
    ------------------
    I've replaced the map api key with mine as well...

    ReplyDelete
  16. Hello,
    How can I add locations to the mapLocations list from the main thread?

    ReplyDelete
  17. Hello com.google not find.... plz help me

    ReplyDelete
  18. Hi....

    I have seen your code in your blog. I implement your code and run the app in my emulator. at that time i get following error message.
    Errors are: 1) "java.io.IOException: Server returned: 3", 2) "at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)", 3) "at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)", 4) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)", 5) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)", 6) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)".

    And only grid is display in emulator.

    Please give me solution of it as soon as early.
    Email is: sneha2011.jobs@gmail.com

    ReplyDelete
  19. file is not available for download please upload the file

    ReplyDelete
  20. Rachana Shah you may use this link:http://gisandroid.blogspot.in/2013/02/sample-android-program-for-google-map.html

    ReplyDelete

Sponser