I am trying to show a location in google map for given lat and longitude. The problem is it is showing default one location and if I navigate to my location (for given lat and long) explicitly then I could see the marker and title on it.
I am expecting as it should show the location of given lat and long in first shot. But by default it is showing some where near greenland.
As i don't have required no of reputations I could not post the images here.
Below is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
double latitude = getIntent().getDoubleExtra("lat", 0);
double longitude = getIntent().getDoubleExtra("lng", 0);
Log.d("Zumbare","lat value : "+latitude);
Log.d("Zumbare","lng value : "+longitude);
LatLng position = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions();
options.position(position);
options.title("Position");
options.snippet("Latitude:"+latitude+",Longitude:"+longitude);
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.addMarker(options);
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(5);
googleMap.moveCamera(updatePosition);
googleMap.animateCamera(updateZoom);
}
Can someone let me know the problem here.
Edit:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
}
Logs:
04-18 12:52:13.535 2566-2581/com.app.maptest.gmap W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-18 12:52:13.536 2566-2581/com.app.maptest.gmap W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9fa6cc00, error=EGL_SUCCESS
04-18 12:52:17.527 2566-2566/com.app.maptest.gmap I/Google Maps Android API﹕ Google Play services package version: 7097470
04-18 12:52:17.556 2566-2566/com.app.maptest.gmap D/Zumbare﹕ lat value : 16.474311
04-18 12:52:17.556 2566-2566/com.app.maptest.gmap D/Zumbare﹕ lng value : 80.712814
04-18 12:52:17.963 2566-2581/com.app.maptest.gmap W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-18 12:52:17.963 2566-2581/com.app.maptest.gmap W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa58ff100, error=EGL_SUCCESS
04-18 12:52:21.485 2566-2697/com.app.maptest.gmap D/﹕ HostConnection::get() New Host Connection established 0xa0593ba0, tid 2697
04-18 12:52:21.516 2566-2566/com.app.maptest.gmap I/Choreographer﹕ Skipped 229 frames! The application may be doing too much work on its main thread.
04-18 12:52:24.522 2566-2578/com.app.maptest.gmap I/art﹕ Background sticky concurrent mark sweep GC freed 2035(112KB) AllocSpace objects, 4(258KB) LOS objects, 0% free, 24MB/24MB, paused 2.269ms total 420.232ms
04-18 12:52:24.666 2566-2578/com.app.maptest.gmap I/art﹕ Background partial concurrent mark sweep GC freed 823(72KB) AllocSpace objects, 6(5MB) LOS objects, 17% free, 18MB/22MB, paused 2.026ms total 139.787ms
Edit2:
package com.app.maptest.gmap;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdate;
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 com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
double latitude = getIntent().getDoubleExtra("lat", 0);
double longitude = getIntent().getDoubleExtra("lng", 0);
Log.d("Zumbare","lat value : "+latitude);
Log.d("Zumbare","lng value : "+longitude);
LatLng position = new LatLng(16.474311,80.712814);
MarkerOptions options = new MarkerOptions();
options.position(position);
options.title("Position");
options.snippet("Latitude:"+latitude+",Longitude:"+longitude);
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.addMarker(options);
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(5);
googleMap.moveCamera(updatePosition);
googleMap.animateCamera(updateZoom);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Aucun commentaire:
Enregistrer un commentaire