Reputation: 7105
I followed the tutorial in
https://developers.google.com/maps/documentation/android/start
and i managed to load a map into my app.Now i want to add some markers into my app using latitudes and longitudes.How do i do it? I tried something but did not work. The code i tried is below.
This how i was going to do it.But i get a null point exception.What is the reason for that?
GoogleMap googleMap = null;
MapFragment fm = (MapFragment) (activity.getFragmentManager())
.findFragmentById(R.id.map);
googleMap = fm.getMap();
double latitude = 7.421226;
double longitude =80.401264 ;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
My layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/filmhall"
android:background="@color/grey" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Upvotes: 2
Views: 22890
Reputation: 1907
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.draggable(true));
check this link
Upvotes: 0
Reputation: 1241
Here my code(it has 4 icons in the map, you just call this class like a fragment in main activity, it will working, REMEMBER: create a picture in drawable and name it is "atmpin2.png"):
package fragments;
import com.einstein.atm.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Marker;
public class FragMapsGoogle extends MapFragment {
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container,
savedInstanceState);
map = getMap();
LatLng place = new LatLng(21, 105.8);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(place, 14.0f));
DisplayMetrics displaymetrics = new DisplayMetrics();
int w,h;
this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
h = displaymetrics.heightPixels;
w = displaymetrics.widthPixels;
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.atmpin2);
float h2w2 = (float)bmp.getHeight()/bmp.getWidth();
GoogleMap map = this.getMap();
Toast.makeText(this.getActivity(), "Size Screen: "+h+"X"+w+" "+h2w2, Toast.LENGTH_LONG).show();
for (int i = 1; i < 5; i++) {
MarkerOptions mkop = new MarkerOptions();
place = new LatLng(21+i*0.00055, 105.8+i*0.00054);
mkop.position(place);
Marker maker = map.addMarker(mkop);
maker.setIcon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(bmp, w/20+5, (int)((w/20+5)*h2w2), true)));
}
return rootView;
}
}
// Ha Noi: 21.0226967,105.8369637
Upvotes: 1
Reputation: 105
// latitude and longitude
double latitude = ;
double longitude = ;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
Upvotes: 1
Reputation: 3339
have a look using this methed we are adding marker on google map
mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));
here is example
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class BasicMapDemoActivity extends FragmentActivity
{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded()
{
if (mMap == null)
{
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null)
{
setUpMap();
}
}
}
private void setUpMap()
{
mMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore"));
// here is marker Adding code
}
}
Upvotes: 5