Displaying google map in android app with specified options -
i'm new android development , had been trying display map in application time now. managed want display specified options such zoom level, location, etc proving difficult.
the following .xml file
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.supportmapfragment"/>
and following .java file
package com.fourapps.cabkonnect; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.view.menu; import android.view.menuitem; import android.widget.toast; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import android.location.location; import android.graphics.color; import com.google.android.gms.maps.model.cameraposition; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polylineoptions; /** * created nanakay on 6/6/13. */ public class home extends fragmentactivity { googlemap map; private static final latlng golden_gate_bridge = new latlng(37.828891,-122.485884); private static final latlng apple = new latlng(37.3325004578, -122.03099823); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home); map = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); if (map == null) { toast.maketext(this, "google maps not available", toast.length_long).show(); } } }
the map displaying want display own options. happy if help. thanks
first have obtain current location:
private location mcurrentlocation; mcurrentlocation = mlocationclient.getlastlocation();
read here know more.
and can animate location using:
latlng mylaln = new latlng(mcurrentlocation.getlatitude(), mcurrentlocation.getlongitude()); cameraposition campos = new cameraposition.builder().target(mylaln) .zoom(15) .bearing(45) .tilt(70) .build(); cameraupdate camupd3 = cameraupdatefactory.newcameraposition(campos); map.animatecamera(camupd3);
i give simple complete example show map , current location:
public class mainactivity extends fragmentactivity implements googleplayservicesclient.connectioncallbacks, googleplayservicesclient.onconnectionfailedlistener { private final static int connection_failure_resolution_request = 9000; private locationclient mlocationclient; private location mcurrentlocation; private googlemap map; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.map); } @override protected void onresume() { super.onresume(); setupmapifneeded(); setuplocationclientifneeded(); mlocationclient.connect(); } private void setupmapifneeded() { // null check confirm have not instantiated // map. if (map == null) { // try obtain map supportmapfragment. map = ((supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map)).getmap(); // check if successful in obtaining map. if (map == null) { toast.maketext(this, "google maps not available", toast.length_long).show(); } } } private void setuplocationclientifneeded() { if (mlocationclient == null) { toast.maketext(getapplicationcontext(), "waiting location", toast.length_short).show(); mlocationclient = new locationclient(getapplicationcontext(), this, // connectioncallbacks this); // onconnectionfailedlistener } } @override public void onpause() { super.onpause(); if (mlocationclient != null) { mlocationclient.disconnect(); } } /* * called location services when request connect client * finishes successfully. @ point, can request current * location or start periodic updates */ @override public void onconnected(bundle databundle) { mcurrentlocation = mlocationclient.getlastlocation(); if (mcurrentlocation != null) { toast.maketext(getapplicationcontext(), "found!", toast.length_short).show(); centerinloc(); } } private void centerinloc() { latlng mylaln = new latlng(mcurrentlocation.getlatitude(), mcurrentlocation.getlongitude()); cameraposition campos = new cameraposition.builder().target(mylaln) .zoom(15).bearing(45).tilt(70).build(); cameraupdate camupd3 = cameraupdatefactory.newcameraposition(campos); map.animatecamera(camupd3); markeroptions markeropts = new markeroptions().position(mylaln).title( "my location"); map.addmarker(markeropts); } /* * called location services if connection location client * drops because of error. */ @override public void ondisconnected() { // display connection status toast.maketext(this, "disconnected. please re-connect.", toast.length_short).show(); } /* * called location services if attempt location services fails. */ @override public void onconnectionfailed(connectionresult connectionresult) { /* * google play services can resolve errors detects. if error * has resolution, try sending intent start google play * services activity can resolve error. */ if (connectionresult.hasresolution()) { try { // start activity tries resolve error connectionresult.startresolutionforresult(this, connection_failure_resolution_request); /* * thrown if google play services canceled original * pendingintent */ } catch (intentsender.sendintentexception e) { // log error e.printstacktrace(); } } else { /* * if no resolution available */ log.e("home", integer.tostring(connectionresult.geterrorcode())); } } }
note1: omitted "check google play services" part simplicity should added practice.
note2: need google-play-services_lib project , reference yours.
you can find information interacting google maps in android here
from google maps documentation referenced above, examples:
zoom controls:
the maps api provides built-in zoom controls appear in bottom right hand corner of map. these enabled default, can disabled calling uisettings.setzoomcontrolsenabled(boolean)
.
my location button:
the location button appears in top right corner of screen when location layer enabled. when user clicks button, camera animates focus on user's current location if user's location known. click trigger googlemap.onmylocationbuttonclicklistener. can disable button appearing altogether calling uisettings.setmylocationbuttonenabled(boolean)
.
add marker:
the below example demonstrates how add marker map. marker created @ coordinates 0,0, , displays string "hello world" in infowindow when clicked.
private googlemap mmap; mmap = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); mmap.addmarker(new markeroptions() .position(new latlng(0, 0)) .title("hello world"));
Comments
Post a Comment